免费注册 查看新帖 |

Chinaunix

广告
  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2910 | 回复: 9
打印 上一主题 下一主题

谁给解释下这几行代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 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]);

论坛徽章:
0
2 [报告]
发表于 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.

论坛徽章:
0
3 [报告]
发表于 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;
复制代码

论坛徽章:
46
15-16赛季CBA联赛之四川
日期:2018-03-27 11:59:132015年亚洲杯之沙特阿拉伯
日期:2015-04-11 17:31:45天蝎座
日期:2015-03-25 16:56:49双鱼座
日期:2015-03-25 16:56:30摩羯座
日期:2015-03-25 16:56:09巳蛇
日期:2015-03-25 16:55:30卯兔
日期:2015-03-25 16:54:29子鼠
日期:2015-03-25 16:53:59申猴
日期:2015-03-25 16:53:29寅虎
日期:2015-03-25 16:52:29羊年新春福章
日期:2015-03-25 16:51:212015亚冠之布里斯班狮吼
日期:2015-07-13 10:44:56
4 [报告]
发表于 2011-06-18 13:06 |只看该作者
后面两个纯粹是因为 $| 对应的文件句柄问题。要先切换默认输出文件再设置 $|,然后再把切换回来

论坛徽章:
0
5 [报告]
发表于 2011-06-18 14:14 |只看该作者
后面两个纯粹是因为 $| 对应的文件句柄问题。要先切换默认输出文件再设置 $|,然后再把切换回来
zhlong8 发表于 2011-06-18 13:06



    数组内外不会隔离作用域吧?(select(F), $| = 1)这个效果是这样{select(F); local($|)=1}还是这样{select(F);$|=1}?求解释~

论坛徽章:
0
6 [报告]
发表于 2011-06-18 19:36 |只看该作者
数组内外不会隔离作用域吧?(select(F), $| = 1)这个效果是这样{select(F); local($|)=1}还是这样 ...
huycwork 发表于 2011-06-18 14:14



    $|是一个特殊变量,它的作用域是当前select的文件句柄。

论坛徽章:
0
7 [报告]
发表于 2011-06-18 20:34 |只看该作者
回复 6# 兰花仙子


    谢谢~

论坛徽章:
0
8 [报告]
发表于 2011-06-19 17:50 |只看该作者
QUOTE:select((select(TO_CHILD), $| = 1))[0]); 后面这个[0]是什么意思?

论坛徽章:
0
9 [报告]
发表于 2011-06-19 18:29 |只看该作者
QUOTE:select((select(TO_CHILD), $| = 1))[0]); 后面这个[0]是什么意思?
chgan100 发表于 2011-06-19 17:50



    (1,2,3)[0]
这个0是什么意思你明白么

论坛徽章:
0
10 [报告]
发表于 2011-06-20 23:13 |只看该作者
少一个右括号啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP