pipe READHANDLE, WRITEHANDLE |
打開一個連接的通信管道:對READHANDLE為的讀取和WRITEHANDLE為寫。您可能需要設置$ |刷新您的WRITEHANDLE的每一個命令後。
0 - 返回
1 - 成功
試試下麵的例子:
#!/usr/bin/perl -w #by www.gitbook.net use IO::Handle; pipe(PARENTREAD, PARENTWRITE); pipe(CHILDREAD, CHILDWRITE); PARENTWRITE->autoflush(1); CHILDWRITE->autoflush(1); if ($child = fork) # Parent code { close CHILDTREAD; # We don't need these in the parent close PARENTWRITE; print CHILDWRITE "34+56;\n"; chomp($result = <PARENTREAD>); print "Got a value of $result from child\n"; close PARENTREAD; close CHILDWRITE; waitpid($child,0); }else{ close PARENTREAD; # We don't need these in the child close CHILDWRITE; chomp($calculation = <CHILDREAD>); print "Got $calculation\n"; $result = eval "$calculation"; print PARENTWRITE "$result\n"; close CHILDREAD; close PARENTWRITE; exit; }
這將產生以下結果:您可以看到,計算發送到CHILDWRITE,然後從CHILDREAD子讀取。然後,結果被計算和發送回父,通過PARENTWRITE,其中父從PARENTREAD讀取結果。
Got 34+56;
Got a value of 90 from child