perl学习之argument
2014-12-27 18:06
134 查看
Arguments are the values you pass to a Perl script. Each value on the command line after the name of the script will be assigned to the special variables$ARGV[0],$ARGV[1],$ARGV[2], and so on. The number of arguments passed to the script is stored in the$#ARGVvariable, and the full argument string is in the variable@ARGV. The name of the currently running program is stored in the$0variable.
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 $0 \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 was: @ARGV \n";
Now if you run this script, 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 that the$#ARGVvariable contained the number of arguments, but I lied--sort of. Since the arguments are numbered starting at zero, you have to add one to the value of$#ARGVto get the actual number of arguments. It's a bit weird, but if you're a fan of the C language, it'll all seem quite normal.
Also note that the@ARGVvariable doesn't start with a dollar sign. That's because it's anarrayvariable, as opposed to the regularscalarvariables we've worked with so far. An array can be thought of as a list of values, where each value is addressed by a scalar (dollar sign) variable and an index 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.
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 $0 \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 was: @ARGV \n";
Now if you run this script, 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 that the$#ARGVvariable contained the number of arguments, but I lied--sort of. Since the arguments are numbered starting at zero, you have to add one to the value of$#ARGVto get the actual number of arguments. It's a bit weird, but if you're a fan of the C language, it'll all seem quite normal.
Also note that the@ARGVvariable doesn't start with a dollar sign. That's because it's anarrayvariable, as opposed to the regularscalarvariables we've worked with so far. An array can be thought of as a list of values, where each value is addressed by a scalar (dollar sign) variable and an index 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.
相关文章推荐
- perl手册学习摘记3
- 【转贴】Perl学习手札
- perl网络编程学习系列:Net:FTP
- perl学习笔记九----特殊变量
- 正则表达式需要灵活理解——Perl学习中
- perl学习笔记十一-----模块
- perl学习笔记一----变量
- perl学习笔记六----函数
- perl学习笔记八----格式化输出
- 【转贴】关于Perl--Perl学习手札
- perl学习笔记 (1)perl常识
- perl学习笔记(一)
- 开始学习perl
- 学习Perl闭包几点心得
- Perl学习手札
- perl学习心得(摘记)
- PERL 学习书库
- perl学习笔记二----列表和数组
- PERL学习笔记[不完整版]
- perl学习心得之一