Chinaunix首页 | 论坛 | 博客
  • 博客访问: 140499
  • 博文数量: 56
  • 博客积分: 245
  • 博客等级: 二等列兵
  • 技术积分: 520
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-08 14:43
个人简介

慢慢来

文章分类

全部博文(56)

文章存档

2017年(5)

2016年(2)

2015年(6)

2014年(28)

2013年(5)

2012年(10)

我的朋友

分类: Python/Ruby

2012-11-07 15:58:16

=================================命令行参数==================================

 
   命令行参数两种操作方法
整体数组法    @ARGV
数组元素法    $ARGV[$i]

    象C一样,PERL也有存储命令行参数的数组@ARGV,$ARGV[$i],与C和shell不同的是,$ARGV[0]是第一个参数,而不是程序名本身
#!/usr/bin/perl
for($j=0;$j<4;$j++)
{
print "$ARGV[$j] \n";
}    
[macg@localhost perltest]$ ./tip.pl 36 gg test a
36               $ARGV[0]==36
gg
test
a  

 
    $tmp=shift(@ARGV); 把数组@ARGV的元素向前移一个,其元素数量即减少了一个
shift是数组操作,不是专门为@ARGV准备的,就当@ARGV是普通数组
注意:两个动作:
    由数组头部,将第一个元素取出,并把这个元素做为返回值
    数组缩短
其实没必要用shift,因为@ARGV可以访问单个数组元素
#!/usr/bin/perl
for($j=0;$j<4;$j++)
{
print "$ARGV[$j] ";
}
print "\n";

$tmp=shift(@ARGV);
print "$tmp\n";
for($j=0;$j<4;$j++)
{
print "$ARGV[$j] ";
}
print "\n";
[macg@localhost perltest]$ ./tip.pl 36 gg test a
36 gg test a
36
gg test a        重新打印参数列(变短了)

 
===================================q指令=====================================

    q------字符串单引号替代
$target = 'This is what you have';    等价于    $target = q[This is what you have];

    qq------双引号替代----在cgi编程时最常用
$target = “This is what you have”;    等价于    $target = qq[This is what you have];
#!/usr/bin/perl
@arraytest="hello "test" world";
如果字符串内也有双引号,比如HTTP语句,就会形成双引号套双引号的问题
foreach  (@arraytest) {
 print $_,"\n";
}   
[macg@localhost perltest]$ ./tip.pl
Bareword found where operator expected at ./tip.pl line 2, near ""hello "test"
        (Missing operator before test?)   
出错
解决:用qq 
#!/usr/bin/perl
@arraytest=qq(hello "test" world);
foreach  (@arraytest) {
 print $_,"\n";
}  
[macg@localhost perltest]$ ./tip.pl
hello "test" world  

 
     qx------反引号替代,相当于取其内字串命令的输出
$run = `ls -la`;    等价于    $run = qx[ls -la];

    qw----串列替代    双引号和逗号替代
#!/usr/bin/perl
@arraytest=("aaa","bbb","123");
foreach  (@arraytest) {
 print $_,"\n";
}
#!/usr/bin/perl
@arraytest=qw(aaa bbb 123);
foreach  (@arraytest) {
 print $_,"\n";
}
[macg@localhost perltest]$ ./tip.pl
aaa
bbb
123
[macg@localhost erltest]$ ./tip.pl
aaa
bbb
123


qw也不一定要用括号,可用任何符号做为起始及结束符号
qw/ Mmm NNN Jjj Kkk Xxx /
qw! Mmm NNN Jjj Kkk Xxx !
qw# Mmm NNN Jjj Kkk Xxx #
qw{ Mmm NNN Jjj Kkk Xxx }
qw< Mmm NNN Jjj Kkk Xxx >
qw[ Mmm NNN Jjj Kkk Xxx ]
但里面都是相同的:都是用空格代替双引号和逗号


========================特殊字符===========================================
 @ARGV 命令行参数   (也是数组)
 $_ 代表缺省变量
 $!      —— 错误讯息内容
 $$   PID
 $0 程序名
 $&  结果
STDIN 标准输入
STDOUT 标准输出
STDERR 标准错误输出

__FILE__ 程序文件名
__PACKAGE__ 目前包名


    $_ 代表缺省变量,语句中缺的变量,可以用$_代替
[macg@localhost perltest]$ vi tip.pl

#!/usr/bin/perl
@arraytest=("aaa","bbb","123");
foreach $tmp (@arraytest) {
 print $tmp,"\n";
}
[macg@localhost perltest]$ ./tip.pl
aaa
bbb
123 
等价于
[macg@localhost perltest]$ vi tip.pl

#!/usr/bin/perl
@arraytest=("aaa","bbb","123");
foreach  (@arraytest) {
 print $_,"\n";
}
[macg@localhost perltest]$ ./tip.pl
aaa
bbb
123
    甚至可以连$_都缺省了,就更简化了
@arraytest=("aaa","bbb","123");
foreach  (@arraytest)
{ print;}
[macg@localhost perltest]$ ./tip.pl
aaabbb123[macg@localhost perltest]$ 
   
       require;           不指定文件名
变量$_的值即作为文件名传递给require
 
   $!      —— 错误讯息内容
open(FHD, "$file") || die "   $! \n"; 
rmdir($tmp[0]) || die "$!";
[macg@localhost perltest]$ ./tip.pl
testdir
0
Directory not empty at ./tip.pl line 13, <> line 2.

   $$      PID
print $$ ,"\n"; 
[macg@localhost perltest]$ ./tip.pl
2861  

  
    $0 程序名,所以$ARGV[0]不是程序名,仍是参数,因为已经有$0代表程序名了
#!/usr/bin/perl
print $0 ,"\n"; 
[macg@localhost perltest]$ ./tip.pl
./tip.pl

  
阅读(2257) | 评论(0) | 转发(0) |
0

上一篇:perl bless 用法实例

下一篇:vi/vim 查找替换

给主人留下些什么吧!~~