in reply to how to access the commandline arguments?
OR you can use the special variable $#ARGV which contains the index number of the last array element. That is, if you have 4 elements in your array the last index number is 3.my $count = @ARGV; print $count;
This has the effect of ensuring your program is passed enough variables.if (@ARGV < 3) { die "use: script var1 var2 var3\n"; } else { my ($var1, $var2, $var3) = @ARGV; } ## then use $var1, $var2 and $var3 in your program
This means if someone calls your program without arguments then a default value is used.my ($stream, $path, $rturn, $depth, $display) = @ARGV; $stream = \*STDOUT unless @_ > 0; $path = cwd unless @_ > 1; $rturn = 'all' unless @_ > 2; $depth = -1 unless @_ > 3; $display = 'rel_file' unless @_ > 4;
Or you can loop through @ARGV like any other array.my $nextvar = shift @ARGV;
In fact you can treat @ARGV pretty much like any array and "push", "shift", "unshift" or any other array function as you please.for my $this (@ARGV) { print "$this\n"; }
... then you see the entire contents of each file output to the screen. If you didn't pass any files then anything passed to STDIN is used.while (<>) { print "$_\n"; }
|
|---|