The argument of Perl learning

Source: Internet
Author: User
Tags scalar perl script

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.