Chinaunix

标题: 谁给解释下这几行代码 [打印本页]

作者: vgra    时间: 2011-06-18 11:14
标题: 谁给解释下这几行代码
pipe(FROM_PARENT, TO_CHILD)   or die "pipe: $!";
pipe(FROM_CHILD, TO_PARENT)   or die "pipe!";
select((select(TO_CHILD), $| = 1))[0]);
select((select(TO_PARENT), $| = 1))[0]);
作者: 兰花仙子    时间: 2011-06-18 11:46
pipe(FROM_PARENT, TO_CHILD)   or die "pipe: $!";
pipe(FROM_CHILD, TO_PARENT)   or die "pipe!";
s ...
vgra 发表于 2011-06-18 11:14



    pipe函数用来建立一对管道:
http://perldoc.perl.org/functions/pipe.html

select那句,表示把对应的句柄的写buffer关闭:
$|

If set to nonzero, forces a flush right away and after every write or print on the currently selected output channel. Default is 0 (regardless of whether the channel is really buffered by the system or not; $| tells you only whether you've asked Perl explicitly to flush after each write). STDOUT will typically be line buffered if output is to the terminal and block buffered otherwise. Setting this variable is useful primarily when you are outputting to a pipe or socket, such as when you are running a Perl program under rsh and want to see the output as it's happening. This has no effect on input buffering. See getc for that. See select on how to select the output channel. See also IO::Handle.

作者: 兰花仙子    时间: 2011-06-18 11:49
select((select(TO_CHILD), $| = 1))[0]);


补充一下,select函数的返回是select之前的旧句柄。
所以上述写法对应于:
  1. my $old_fh = select TO_CHILD;
  2. $|=1;  # 关闭TO_CHILD的buffer
  3. select $old_fh;
复制代码

作者: zhlong8    时间: 2011-06-18 13:06
后面两个纯粹是因为 $| 对应的文件句柄问题。要先切换默认输出文件再设置 $|,然后再把切换回来
作者: huycwork    时间: 2011-06-18 14:14
后面两个纯粹是因为 $| 对应的文件句柄问题。要先切换默认输出文件再设置 $|,然后再把切换回来
zhlong8 发表于 2011-06-18 13:06



    数组内外不会隔离作用域吧?(select(F), $| = 1)这个效果是这样{select(F); local($|)=1}还是这样{select(F);$|=1}?求解释~
作者: 兰花仙子    时间: 2011-06-18 19:36
数组内外不会隔离作用域吧?(select(F), $| = 1)这个效果是这样{select(F); local($|)=1}还是这样 ...
huycwork 发表于 2011-06-18 14:14



    $|是一个特殊变量,它的作用域是当前select的文件句柄。
作者: huycwork    时间: 2011-06-18 20:34
回复 6# 兰花仙子


    谢谢~
作者: chgan100    时间: 2011-06-19 17:50
QUOTE:select((select(TO_CHILD), $| = 1))[0]); 后面这个[0]是什么意思?
作者: 兰花仙子    时间: 2011-06-19 18:29
QUOTE:select((select(TO_CHILD), $| = 1))[0]); 后面这个[0]是什么意思?
chgan100 发表于 2011-06-19 17:50



    (1,2,3)[0]
这个0是什么意思你明白么
作者: ustbleetom    时间: 2011-06-20 23:13
少一个右括号啊




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2