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.

perl学习之argument的更多相关文章

  1. perl学习笔记---标量

    1.perl 输出时,使用 逗号,连接多个字符串 如:print “The answer is ”,6*7, “.\n” 2.当一个字符串由双引号括起来时,如果变量前没有反斜线,则变量会被其值内插 $ ...

  2. perl学习之路1

    一切要从Hollo world开始 公司要用perl....啊, 不会只能自学了, 毕竟是公司啊, 不是学校...公司不学习就滚蛋了...惨惨惨 因为是学习嘛, 感觉开虚拟机比较麻烦所以直接用了个 瘟 ...

  3. Perl 学习笔记-高级控制结构

    1.unless控制结构 类似于独立的else语句; 要么条件为真, 要么执行语句块内的代码;  unless(<condition>){code...;} 等价于  if(<con ...

  4. Perl 学习笔记-标量数据

    最近学习Perl, 准备看一遍入门指南,关键的东西还是记录下来,以便以后复习和查看参考. 笔记来自<<Perl语言入门第5版>> 1. 在Perl内部,不区分整数值和浮点数值, ...

  5. perl学习笔记一

    标量数据 标量:数字.字符.可以存储在标量变量中也可以从文件和设备中读取. 数字:所有数字内部格式相同——双精度浮点数. 浮点数直接量:程序员在程序中直接键入的数字. 整数直接量:6129804028 ...

  6. perl学习之:use and require

    本文和大家重点学习一下Perl use和require用法对比,这两个函数都是一个意思,加载和引用Perl的模块,或者是子程序,区别在于Perl use是在当前默认的里面去寻找,一旦模块不在指定的区域 ...

  7. perl学习之路3

    Perl编程之路3 标签: perl 列表与数组   Perl里面代表复数的就是列表和数组 列表(list)指的是标量的有序集合, 而数组(array)则是存储列表的变量. 在Perl这两个属于尝尝混 ...

  8. perl学习之路2

    这些主要是从 "小骆驼" 书上粘贴或者摘抄出来的, 个人认为需要记的语法知识 "在某些情况下, 你可能需要在一台机器上写程序, 再传送到另一台机器上运行.这时候, 请使用 ...

  9. perl学习笔记

    一.正则表达式 匹配一个文件中的某个单词,并打印出来 #!/usr/bin/perl use strict; use warnings; ; open(FILE, "< temp.pl ...

随机推荐

  1. VC 中TEXT、_T、L的区别

    http://i.cnblogs.com/EditPosts.aspx?opt=1 对于从VC++6.0转到VS2005编译环境中的程序员.往往会碰到字符集之间的转换. VC6.0采用的是ANSI字符 ...

  2. For循环执行AFNetworking请求

    屏幕快照 2017-12-19 下午1.46.25.png 需求:如下操作打印的文档为 NSLog(@"开始");for(NSIntegeri =0; i <5; i++) ...

  3. JDBC事务之例子篇

    上一篇随笔记了一些有关JDBC事务管理的理论知识.这篇来看例子(主要怕一篇随笔装所有东西太长了然后分开呵呵) 一般讲事务管理的,都是拿转钱来当例子的,嗯没错我们这也是. 这个是数据库中的t_accou ...

  4. Less学习(2)(完结)

    七.模式匹配与Guard表达式 根据传入参数的不同,引入不同的属性集. .mixin (dark, @color) { color: darken(@color, 10%); } .mixin (li ...

  5. 安卓新的联网方式 Volley的使用(2)

    如果使用volley 获取 网络图片 最好还是用 Volley 提供的NetworkImageView类,  可以很轻松的 完成工作, 而且他可以设置缓存, lru 和 sd卡的缓存.一些都封装好了. ...

  6. hdu4832Chess(dp)

    链接 这题第一想法是矩阵,不过范围太大了,然后就没有思路了.. 之后看到群里的解法,行和列可以分着走,两者是互不影响的,这样就把二维转换成了一维,直接dp求出就可以了. 然后再组合相乘一下. #inc ...

  7. DBAplus社群线上分享----Sharding-Sphere之Proxy初探

    功能 Cobar Mycat Heisenberg Shark TDDL Sharding-JDBC 是否开源 开源 开源 开源 开源 部分开源 开源 架构模型 Proxy架构 Proxy架构 Pro ...

  8. Unity 360 旋转 缩放

    using UnityEngine; using System.Collections; public class SandR : MonoBehaviour { public GameObject ...

  9. Gitlab User Guide

    Installation Configuration Set user name and email Add SSH keys Repository Create New Repository Clo ...

  10. IOS中Llabel整理

    ·UILable是iPhone界面最基本的控件,主要用来显示文本信息.·常用属性和方法有:1.创建CGRect rect = CGRectMake(100, 200, 50, 50);UILabel ...