2013-07-03 53 views

回答

2

你可以打電話給你希望的任何命令下面的代碼。它是在反引號通常做了簡化:

my $output = `make`; 
print($output); 

另一種常見的方法是打開一個程序讀取就像一個文件:

my $filehandle; 
if (! open($filehandle, "make |")) { 
    die("Failed to start process: $!"); 
} 
while (defined(my $line = <$filehandle>)) { 
    print($line); 
} 
close($line); 

這樣做的好處是,你可以看到輸出,因爲它是從過程中交付的。

您可能希望通過增加2>&1到命令行來捕獲STDERR輸出以及輸出STDOUT:

my $filehandle; 
if (! open($filehandle, "make 2>&1 |")) { 
    die("Failed to start process: $!"); 
} 
while (defined(my $line = <$filehandle>)) { 
    print($line); 
} 
close($line); 
+0

感謝..快速回復! –

2

你只需要使用反引號。

my $command = `make`; 
print $command; 

返回值system是退出狀態。 見here

編輯:鏈接到系統