1. 程式人生 > >perl open函式開啟管道

perl open函式開啟管道

語法5:open(filehandle,"|unix command")
說明:就會把在filehandle的資料輸入給unix的指令來作處理。
例項1 發郵件

$mailprog="/usr/ucb/mail"; #unix系統上的寄信程式(一定要加絕對路徑)
$who="[email protected]";
$open(file,"|$mailprog $who")||die"開啟失敗\n";
print file "I love you!\n";
print file "I want to see you.\n";
close(file);
就會通過unix系統mail的程式,將FILE這個FILEHANDLE的資料內容寄給$who這個變數所指定的收信人。 我們可以利用open這個函式來設計一個來信批評CGI應用程式,在本書中的下一章中會有詳細的介紹。
==============================
例項2 顯示系統程序數
#!/usr/bin/perl
# returns number of open processes from 'ps' output
open(PROCS,'/bin/ps ax |');
while () {
        $procs++;
}
close(PROCS);
$procs--;
print $procs;
===============================
#下面的小例子 僅僅是驗證perl | +unix命令的可能性!
[
[email protected]
perl]# cat catmai
#!/usr/bin/perl
$catprog="/bin/cat";
open(file,"|$catprog >new")||die"開啟失敗\n";
print file "i love you\n";
print file "i want to see you\n";
close(file);

[[email protected] perl]# cat new
i love you
i want to see you
[[email protected] perl]#