首页 技术 正文
技术 2022年11月16日
0 收藏 438 点赞 3,292 浏览 1500 个字

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.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898