Friday, February 4, 2011

PERL : Positional Prameters

pico posparam1.pl
#!/usr/bin/perl -w
# scalar, array, hashes - are stored in @ARGV
print "$ARGV[0]\n"
#end
./posparam1.pl # Prints -> nothing prints
./posparam1.pl 1 # Prints -> 1
./posparam1.pl testarg # Prints -> testarg
./posparam1.pl testarg testarg2 # Prints -> testarg


#!/usr/bin/perl -w
print "$#ARGV\n" #prints the last number
#end
./posparam1.pl # Prints -> -1 . If no values are given ARGV is set to -1
./posparam1.pl testarg # Prints -> 0
./posparam1.pl testarg testarg2 # Prints -> 1


#!/usr/bin/perl -w
$#ARGV += 1;
print "This scrips accepts $#ARGV arguments\n" #prints the last number
#end
./posparam1.pl # Prints -> 0
./posparam1.pl testarg # Prints -> 1
./posparam1.pl testarg testarg2 # Prints -> 2


#!/usr/bin/perl -w
$REQPARAM = 3;
$BAGARGS=165;
$#ARGV += 1;
unless ($#ARGV == 3)
{
print "$0 requires $REQPARAM arguments"; # $0 the name of the script
exit $BADARGS;
}
print "This scrips accepts $#ARGV arguments\n" #prints the last number
#end
./posparam1.pl # Prints -> posparam1.pl requires 3 parameters
echo $? -> returns 165
perldoc -f exit # documentation about function exit


#!/usr/bin/perl -w
$REQPARAM = 3;
$BAGARGS=165;
$#ARGV += 1;
unless ($#ARGV == 3)
{
print "$0 requires $REQPARAM arguments"; # $0 the name of the script
exit $BADARGS;
}
print "$ARGV[0] $ARGV[1] $ARGV[2]\n"; is the same with print "$ARGV[0..$#ARGV]\n";
#end
If you give 3 parameters the result for echo $? is 0

No comments:

Post a Comment