Perl运行其他程序的5种方法

前端之家收集整理的这篇文章主要介绍了Perl运行其他程序的5种方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转自:http://www.cnblogs.com/tobecrazy/p/perl.html

1.使用system函数 运行成功,返回0,运行失败则返回非负整数

system(“cmd”);

2.使用qx

my $cmd1=qx/date/;

3.使用“ 与qx等效

4.使用open函数

open(CMD,“ifconfig |”) or die $!

my @result=;

close(CMD);

5.使用readpipe函数

使用readpipe函数可以获取外部程序运行的结果,比如运行 ls 会列出当前目录的文件文件夹,

my $result=readpipe(“ls “);

#!/usr/bin/perl
use strict;
my $return=system("date");
print "this is system function,successful:return $return\n";
$return=system("not_exist");
print "this is sysyem function Failed,return $return\n";
#use qx
my $cmd1=qx/pwd/;
print "this time use qx $cmd1";
#qx=``
my $cmd2=`pwd`;
print "this time use \`\` $cmd2 ";
open(CMD,"echo \"\$(date +\%Y\%m\%d)\"|") or die $!;
my @result=<CMD>;
close(CMD);
print @result;
my $result=readpipe("ls -rtl");
print $result;

猜你在找的Perl相关文章