Arguments is the values you pass to a Perl script. Each value on the command line after the name of the script is assigned to the special variables$ARGV [0], $ARGV [1], $ARGV [2], and so on. The number of arguments passed to the script was stored in the $ #ARGVvariable, and the full argument string was in T He variable @ARGV. The name of the currently running program was stored in the $ variable.
Let's try some examples working with arguments and other special variables. Create an executable script calledtestvars.plcontaining these lines:
#!/usr/bin/perl
Print "My name is $ \ n";
Print "First arg is: $ARGV [0] \ n";
Print "Second arg is: $ARGV [1] \ n";
Print "Third arg is: $ARGV [2] \ n";
$num = $ #ARGV + 1; Print "How many args?" $num \ n ";
Print "The full argument string is: @ARGV \ n";
Now if you run the this script and here's what you'll see:
$./testvars.pl dogs can whistle
My name is testvars.pl
First ARG is:dogs
Second Arg is:can
Third Arg is:whistle
How many args? 3
The full argument string was:dogs can whistle
Just a few notes about that example. I did say then the$ #ARGVvariable contained the number of arguments, but I lied--sort of. Since the arguments is numbered starting at zero and you had to add one to the value of$ #ARGVto get the actual num ber of arguments. It's a bit weird, but if you ' re a fan of the C language, it's all seem quite normal.
Also Note the@ARGVvariable doesn ' t start with a dollar sign. That's because it's anarrayof variable, as opposed to the regularscalarvariables we ' ve worked with so far. An array can being thought of as a list of values, where each value are addressed by a scalar (dollar sign) variable and an in Dex number in square brackets, as in$ARGV [0],$ARGV [1], and so on. Don ' t worry too much about arrays for Now--that's a topic for more study on your own.
The argument of Perl learning