Perl语言入门(11 系统之间互操作)

由于公司需要,本人最近在学习Perl这种脚本语言,本文是我在学习Perl的过程中总结出来的一些心得和笔记,希望能够帮助也在学习Perl的各位同僚。废话不多说直接上干货!!!

————————————————— 干货分割线 —————————————————

1.System()函数

system command;

若要运行非P e r l的命令,可以使用该函数。command是要运行的命令,正常运行返回0,非正常运行返回非0值.

Exp:

a.[UNIX系统下]:

System(“ls -lF”);
if(system(“perldoc -f system”)){
   Print “Your documentation isn’t installed \n”;
}

[DOS/WIN]:

System(“dir /w”);
If(system(“perldoc -f system”)){
   Print “Your documentation isn’t installed \n”;
}


b.[UNIX系统下]:

$file = “myfile.txt”;

System(“vi $file”);

 

[DOS/WIN]:

$file = “myfile.txt”;

System(“edit $file”);

 

c.[UNIX系统下]:

System(“xclock -update 1”);

 

d.[DOS/WIN]:

System(“notepad.exe myfile.txt”);

 

e.捕获文件输出:

system(perldoc perlfaq5 > faqfile.txt);

 

f.按照文件中的名字排序,并输出

system(sort $f | lp);

 

g.后台运行xterm命令

system(xterm $);

 

2.捕获输出

[原始版]

System(“dir > outfile”);

Open(OF,”outfile”);

@data = <OF>;

Close(OF);

 

[升级版]

$dir = `dir`;

<===>   $dir = qx{dir};

PS:

1.``等价于qx{}.任何字符均可用来取代{ },成对的字符如< >,()和[ ]等均可以使用。

2.<===> 表示等价于符号,本人自创,源于数学中,下同

 

$complex = `sort \’grep -l’conf’*\``;

  <===>$complex = qx{sort ’grep -l’conf’*`};

 

为了避免外部系统变量和perl内部变量混淆,可以在不想让P e r l进行内插替换的变量前面加上一个反斜杠,如:

$myhome = `ls \$HOME;

$windows = `dir %windir%`;

现在,$HOME是UNIX shell的HOME变量,% windir%是commcand.com的winddir变量。另一种方法是用q x { }表示法来代替反引号,并用单引号来限定q x,如:

$myhome = qx’ls $HOME’;

$windows = qx’dir %winddir%’;

PS:

PERL将qx’ ’视为特殊标号,且不展开它里面的P e r l变量,因此,你可以使用反引号,并且不必用反斜杠对命令中出现的其他反引号进行转义。

 

3.管道

[原始版]

Dir >outfile

Sort outfile > newfile

More newfile

d i r的输出在outfile中集中,然后使用sort对outfile排序,同时sort的输出被存放在newfile中。接着m o r e命令显示newfile的内容,每次显示一屏内容。

管道允许你执行与上面相同的命令序列,但是不带o u t f i l e和n e w f i l e,如:

[升级版]

dir |sort|more

d i r的输出被赋予s o r t,然后s o r t对数据进行排序。s o r t的输出被赋予m o r e每次显示1页。它不需要重定向(>)或临时文件。

这种命令行称为管道命令行,两个命令之间的竖线称为管道

Exp:

#!/usr/bin/perl
use strict;  
use warnings;
 
my $dir;
open(RHANDLE,"dir /B|sort |");
while($dir = <RHANDLE>){
    print "$dir\n";
}

4.可移植性

Perl可以在任何系统上运行,但是要遵守一定的原则:

• 始终使警告特性处于打开状态,并使用use strict命令。

• 始终都要检查来自系统请求的返回值,如:应该用open || die,而不使用open。

• 输出表义性强的出错消息。

• 使用P e r l的内置函数,执行你要用s y s t e m函数或反引号(` `)来执行的操作。

• 将依赖系统执行的操作(文件I/O,终端I/O、进程控制等)封装在函数中,检查以确保

这些操作受当前操作系统的支持。

 

Exp:

寻找可用的磁盘空间

[WIN系统]

#!/usr/bin/perl
use strict;  
use warnings;
 
my (@dir,$free);
@dir = `dir`;
$free = $dir[$#dir];
$free =~ s/.*([\d,]+)\w+ free/$1/;  #.*任何数值.\d数字
$free =~ s/,//g;
print $free;

可能输出:

PS:上面这个代码段取出@ dir中的目录列表的最后一行信息,使用正则表达式删除不包括数字(即bytes free前面的数字和逗号)在内的其他信息。最后逗号被删除,这样,$ f r e e只包含原始的空闲磁盘空间.

[LINUX系统]

#!/usr/bin/perl
use strict;  
use warnings;
 
my (@dir,$free);
@dir = `df -k .`;
$free = (split(/\s+/,$dir[$#dir]))[3]; 
$free = $free * 1024;
print $free;

[包括DOS/WIN和Linux]

#!/usr/bin/perl
use strict;  
use warnings;
 
Sub freespace{
    my (@dir,$free);
    if($^o eq 'MSWin32'){
        @dir = `dir`;
        $free = $dir[$#dir];
        $free =~ s/.*([\d,]+)\w+ free/$1/; 
        $free =~ s/,//g;
    }elsif($^o eq 'linux'){
        @dir = `df -k .`;
        $free = (split(/\s+/,$dir[$#dir]))[3]; 
        $free = $free * 1024;
    }elsif{
        $free = 0;
        warn "Cannot determine free space on this machine\n";
    }
    return $free;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值