IPC::Open3 ã®æ£ãã使ãæ¹ (re .pl 㪠config ãã¡ã¤ã«ã®ã³ã³ãã¤ã«ãã¨ããããã§ãã¯ãã¦ã¿ã - TokuLog æ¹ã¡ tokuhiromâs blog)
http://d.hatena.ne.jp/tokuhirom/20100813/1281666615 ã®ä»¶ã§ãããIPC::Open3 ã®ä½¿ãæ¹ãæ°ã«ãªã£ãã®ã§ãåæã«æ·»å (c) dankogaiã
use IPC::Open3; ... my($wtr, $rdr, $err); my @cmd = ($^X, (map { "-I$_" } grep { !ref $_ } @INC), '-c', $file); my $pid = open3($wtr, $rdr, $err, @cmd); waitpid($pid, 0);
IPC::Open3 ã¯åããã»ã¹ã®æ¨æºå ¥åºåã¨æ¨æºã¨ã©ã¼åºåãã親ããã»ã¹ã¨ãã¤ãã§ã¤ãªãã§ããã»ã¹ééä¿¡ãè¡ãããã®ä»çµã¿ã§ãã
ãã¤ãã«ã¯ä¸å®ãµã¤ãºã®ãããã¡ããã (Linux ã 㨠4KB)ããã¤ãã«ãããè¶ ãã大ããã®ãã¼ã¿ãæ¸ãè¾¼ã¾ããã¨ãèªã¿è¾¼ã¿å´ã®ããã»ã¹ããã¼ã¿ãèªãã¾ã§ãæ¸ãè¾¼ã¿å´ã®ããã»ã¹ã¯ãããã¯ããã¾ãã
ã¤ã¾ããä¸ã®ã³ã¼ãã¯ãåããã»ã¹ããã®æ¨æºåºåãæ¨æºã¨ã©ã¼åºåã«ãã¤ãã®ãããã¡ãµã¤ãºä»¥ä¸ã®å¤§ããã®ãã¼ã¿ãæ¸ããå ´åã«ããã¤ã¾ã§ãçµäºããªãï¼ãããããã¯ããï¼ãã¨ã«ãªãã¾ãã
ã§ã¯ãã©ãæ¸ãã°ããã®ã§ãããã
my $pid = open3($wtr, $rdr, $err, @cmd); close $rdr; close $err; waitpid($pid, 0);
ã§ããããï¼
éãã¾ããããã ã¨ãåããã»ã¹ãã¯ãã¼ãºãããæ¨æºåºåï¼ãããã¯æ¨æºã¨ã©ã¼åºåï¼ã«æ¸ãè¾¼ããã¨ããç¬éã«ãSIGPIPE ãçºçãããã¨ã«ãªãã¾ããSIGPIPE ã®ããã©ã«ãã§ã®åä½ã¯ããã»ã¹ã®çµäºãªã®ã§ãåããã»ã¹ã¯ç°å¸¸çµäºãããã¨ã«ãªãã¾ãã
ããã§ã¯ãwaitpid ããåã«ã
() = <$rdr>; () = <$err>;
ã¨ããã°ããã®ã§ããããï¼
ã¾ã éãã¾ããããã ã¨ãåããã»ã¹ãæ¨æºã¨ã©ã¼åºåã«ãããã¡ãµã¤ãºãè¶ ããæ¸ãè¾¼ã¿ãè¡ã£ãå ´åã«ã親ããã»ã¹ã¯åããã»ã¹ã®æ¨æºåºåããèªã¿ãããã¨ããã¾ã¾ãåããã»ã¹ã¯æ¨æºã¨ã©ã¼åºåã«åºåãããã¨ããã¾ã¾ã®ç¶æ ã§ããããããã¯ã«é¥ãã¾ãã
æ£è§£ã¯ãIPC::Open3 ã®æ¬¡ã®ãããªä»æ§ãå©ç¨ãã¦ã
If CHLD_ERR is false, or the same file descriptor as CHLD_OUT, then STDOUT and STDERR of the child are on the same filehandle.
my $pid = open3($wtr, $rdr, 0, @cmd); () = <$rdr>; waitpid($pid, 0);
ã¨ããå½¢ã§ã*1ãopen3 ã®ç´å¾ã« close $wtr ã¨ããã¨ããããè¡åãããããããã¾ãããã
以ä¸ããã¼ãã«ã®ä¸ããç®ç·ã§ã®ã¤ã¶ããã§ããã
18:13追è¨:
ããã®ã¯exit codeãªã®ã ããããªãã¤ã¬ã¯ãã§æ¨ã¦ã¡ãé§ç®ãªã®ï¼
はてなブックマーク - mattnのブックマーク / 2010年8月13日
ã¯ããä»åã®å ´åã¯èªã¿æ¨ã¦ã¦ããã ããªã®ã§ãIPC::Open3 ã®ä»¥ä¸ã®æ©è½ãå©ç¨ãã¦ã
If CHLD_IN begins with "<&", then CHLD_IN will be closed in the parent, and the child will read from it directly. If CHLD_OUT or CHLD_ERR begins with ">&", then the child will send output directly to that filehandle. In both cases, there will be a dup(2) instead of a pipe(2) made.
my $pid = open3( '<&' . File::Spec->devnull, '>&' . File::Spec->devnull, 0, @cmd, ); waitpid($pid, 0);
ã¨ãã¦ãããã¨æãã¾ãã
*1:ã©ããã¦ãæ¨æºåºåã¨æ¨æºã¨ã©ã¼åºåãèªã¿åãããå ´åã¯ãselect ã使ã£ã¦èªã¿è¾¼ã¿å¯è½ãªæ¹ã®ãã¡ã¤ã«ãã³ãã«ããèªããã¨ããã³ã¼ããæ¸ãå¿ è¦ãããã¨æãã¾ã