perl 学习笔记

1795阅读 0评论2011-01-17 zzy7186
分类:LINUX

如果没有在perl脚本中第一行写shbang (#!/usr/bin/perl -w) 可以通过命令行执行脚本
perl urlscript
可以通过perl -c urlscript 来检查perl的脚本语法
命令行执行perl
-e 选项允许perl从命令行执行perl语句而不是从脚本。
perl -e 'print qq/hello world\n/;'
这里的qq/hello world\n/ 相当于"hello world\n"
-n 选项允许用户遍历整个文件 或标准输入 
#date | perl -ne 'print;'
2010年 11月 30日 星期二 08:41:51 CST

数组变量:
$#arrayname 是数组最后一个元素的下标,默认情况下perl中的数组下标是从0开始的,所以$#arrayname+1 为真正数组元素的个数。
但是也可以通过改变特殊变量$[的值改变数组的起始下标
测试程序及结果:
  1. #!/usr/bin/perl -w
  2. $[=1;
  3. @test=qw(one two three);
  4. print "\$test[1]=$test[1]\n";
  5. print '$#test is ',$#test,"\n";
  6. print "--------------------\n";
  7. $[=0;
  8. print "\$test[1]=$test[1]\n";
  9. print '$#test is ',$#test,"\n";
结果:

  1. [root@PC_IN_LAN learnperl]#./a
  2. $test[1]=one
  3. $#test is 3
  4. --------------------
  5. $test[1]=two
  6. $#test is 2
  7. 要取得数组最后一个元素可以用-1这个索引$arrayname[-1]
上一篇:perl 中的localtime 函数
下一篇:【转】perl 中的几个函数 map seek read getc 等