2011-08-22 57 views
1

如果我通过子例程调用它,如何在perl中执行shell命令?如何在perl子例程内执行shell命令?

例如,我可以在命令行中运行此得到一个特定的值:

mycert-util --show test.user.myuser_cd 

我想运行一个Perl子程序中这个命令并调用它。例如,我的例程是get_auth,我想打印出get_auth的值。

代码:

use strict; 
use warnings; 

#&get_auth; 
#get_auth(); 

print "The value is: get_auth() \n"; 

sub get_auth 
{ 
    $exec=`mycert-util --show test.user.myuser_cd`; 
} 
+0

您显示的代码没有按照您的要求工作吗?如果没有,你期望的行为是什么,它做什么呢? –

+2

顺便说一句,perl不会在双引号字符串中插入子例程调用,所以它应该是“”值为:“.get_auth()。”\ n“'。 (它必须在子定义被识别之后才会出现)。但是这与子例程中的shell命令无关。 –

回答

3

你的问题是不是子,但你怎么称呼它。您不能将子呼叫放在引号内。下面是一些等同替代功能。

use feature qw(say); 
say "The value is: ", get_auth(); 

sub get_auth { 
    return qx(mycert-util --show test.user.myuser_cd); 
} 
+0

perl 5.8中的'feature'和'say'? – jdamae

+0

我会这样认为的。试试看。 – TLP

+0

没有运气。 '不能在@ INC中定位feature.pm' – jdamae

2

看起来你可能需要回到你的$ EXEC变量,然后将它输出正确:

sub get_auth 
{ 
    $exec=`mycert-util --show test.user.myuser_cd`; 
    return $exec; 
} 

print "The value is: " . get_auth() . "\n";