免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5256 | 回复: 11
打印 上一主题 下一主题

【求教】请教一个很奇怪的问题,真的很奇怪 [复制链接]

论坛徽章:
1
未羊
日期:2014-09-08 22:47:27
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-04-13 09:18 |只看该作者 |倒序浏览
本帖最后由 wxlfh 于 2014-04-15 09:36 编辑
  1. #-- Test.pm
  2. package Test;

  3. sub get_row {
  4.     my $file = $_[0];
  5.     open my $fh, "<", $file or return;
  6.     return sub {
  7.         my $row = <$fh>;
  8.         return $row;
  9.     }
  10. }
复制代码
  1. # 这样调用输出为空
  2. use strict;
  3. use lib 'C:/Test';
  4. my $row = Test::get_row($0);
  5. while ( $row->() ) { print; }
复制代码
  1. # 这样调用输出正常
  2. use strict;
  3. use lib 'C:/Test';
  4. my $row = Test::get_row($0);
  5. while ( my $line = $row->() ) { print $line; }
复制代码
百思不得其解,请高手指教,谢谢!!
忘记说了,我的Perl版本是草莓发行版 5.18.1,32位,操作系统是Windows XP sp3。

求职 : 软件工程师
论坛徽章:
3
程序设计版块每日发帖之星
日期:2015-10-07 06:20:00程序设计版块每日发帖之星
日期:2015-12-13 06:20:00程序设计版块每日发帖之星
日期:2016-05-05 06:20:00
2 [报告]
发表于 2014-04-13 09:47 |只看该作者
局部变量的引用,不能用 $_; 好好写代码吧,别再追求酷了,要不就该@哭@了。

论坛徽章:
1
未羊
日期:2014-09-08 22:47:27
3 [报告]
发表于 2014-04-13 09:52 |只看该作者
看不懂,真心请教,能不能举个例子说明一下?
104359176 发表于 2014-04-13 09:47
局部变量的引用,不能用 $_; 好好写代码吧,别再追求酷了,要不就该@哭@了。

求职 : 软件工程师
论坛徽章:
3
程序设计版块每日发帖之星
日期:2015-10-07 06:20:00程序设计版块每日发帖之星
日期:2015-12-13 06:20:00程序设计版块每日发帖之星
日期:2016-05-05 06:20:00
4 [报告]
发表于 2014-04-13 10:05 |只看该作者
本帖最后由 104359176 于 2014-04-13 10:06 编辑

http://perldoc.perl.org/perl5180 ... is-now-experimental

Lexical $_ is now experimental
Since it was introduced in Perl v5.10, it has caused much confusion with no obvious solution:
Various modules (e.g., List::Util) expect callback routines to use the global $_ . use List::Util 'first'; my $_; first { $_ == 1 } @list does not work as one would expect.
A my $_ declaration earlier in the same file can cause confusing closure warnings.
The "_" subroutine prototype character allows called subroutines to access your lexical $_ , so it is not really private after all.
Nevertheless, subroutines with a "(@)" prototype and methods cannot access the caller's lexical $_ , unless they are written in XS.
But even XS routines cannot access a lexical $_ declared, not in the calling subroutine, but in an outer scope, iff that subroutine happened not to mention $_ or use any operators that default to $_ .
It is our hope that lexical $_ can be rehabilitated, but this may cause changes in its behavior. Please use it with caution until it becomes stable.

论坛徽章:
1
未羊
日期:2014-09-08 22:47:27
5 [报告]
发表于 2014-04-13 10:21 |只看该作者
谢谢楼上。

论坛徽章:
0
6 [报告]
发表于 2014-04-13 11:50 |只看该作者
test this code
输出: 百思不得其解.....
  1. use strict;
  2. use lib 'C:/Test.pm';
  3. my $row = Test::get_row($0);
  4. $_ = "百思不得其解\n";  # for test
  5. while ( $row->() ) { print; }
复制代码
print;    # => print $_;

while ( $row->() ) { print; }
# while $row->() true do print $_   
# $_ != $row->()
# $_ no value => 输出为空

# 这样调用输出正常

while ( $_ = $row->() ) { print; }
# $_ = $row->()
# if $_ true then print $_
# print $_;

求职 : 软件工程师
论坛徽章:
3
程序设计版块每日发帖之星
日期:2015-10-07 06:20:00程序设计版块每日发帖之星
日期:2015-12-13 06:20:00程序设计版块每日发帖之星
日期:2016-05-05 06:20:00
7 [报告]
发表于 2014-04-13 12:12 |只看该作者
回复 6# ba_du_co


    This is new "特性" of Version 5.018;

论坛徽章:
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
8 [报告]
发表于 2014-04-13 15:05 |只看该作者
回复 4# 104359176


    你贴这个不太合适,不建议的是 my $_ ,这个我都没见人用过。我们平时用的都是全局变量 $_ 即 $main::_ 用这个没什么问题,你严格要求自己不用 $_ 也行。

    他的问题是认为 while 会把条件判断的值赋值给 $_,事实并不是这样,while 只是特殊处理 while (<sth>){} 或 ... while <sth> 这个固定模式,会自动插入代码变成 while (defined($_ = <sth>)){}  见 perop #I/O-Operators

论坛徽章:
1
未羊
日期:2014-09-08 22:47:27
9 [报告]
发表于 2014-04-13 18:28 |只看该作者
本帖最后由 wxlfh 于 2014-04-13 18:34 编辑

版主的建议是中肯的,我以前就是这样认为的。

求职 : 软件工程师
论坛徽章:
3
程序设计版块每日发帖之星
日期:2015-10-07 06:20:00程序设计版块每日发帖之星
日期:2015-12-13 06:20:00程序设计版块每日发帖之星
日期:2016-05-05 06:20:00
10 [报告]
发表于 2014-04-14 08:10 |只看该作者
回复 8# zhlong8


    你说的很对,我的帖子岂止是不适合,简直就是误导。我个人基本不用这个变量。怕过段时间,自己看不懂。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP