加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl的参数讨论

发布时间:2020-12-16 00:29:44 所属栏目:大数据 来源:网络整理
导读:Arguments and Other Special Variables Arguments are the values you pass to a Perl script. Each value on the command line afterthe name of the script will be assigned to the special variables $ARGV[0] , $ARGV[1] , $ARGV[2] ,and so on. The n
Arguments and Other Special Variables

Arguments are the values you pass to a Perl script. Each value on the command line afterthe 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$#ARGV variable,and the full argument string is in the variable @ARGV. The name ofthe currently running program is stored in the $0 variable.

Let's try some examples working with arguments and other special variables. Create anexecutable script called testvars.pl containing 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 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 $#ARGV variable contained thenumber of arguments,but I lied--sort of. Since the arguments are numbered starting atzero,you have to add one to the value of $#ARGV to get the actual number ofarguments. It's a bit weird,but if you're a fan of the C language,it'll all seem quitenormal.

Also note that the @ARGV variable doesn't start with a dollar sign. That's because it'san array variable,as opposed to the regular scalar variables 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 formore study on your own.


Read more: http://lowfatlinux.com/linux-perl-arguments.html#ixzz1ho9kVUaK

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读