大佬教程收集整理的这篇文章主要介绍了perl – 防止字符串被解释为文件句柄,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
# let this be some nice class I wrote package Input { sub awesome { ... } }
所以当我们做输入 – >真棒或非常小心:’输入’ – >真棒,该方法将被调用.除非:
# now somewhere far,far away,in package main,somebody does this: open Input,"<&",\*STDIN or die $!; # normally we'd open to a file
这个代码甚至不必被执行,但是只有解析器才能看到Perl从现在开始将字符串“Input”解释为文件句柄.因此,一个方法调用’Input’ – > awesome将会死,因为表示文件句柄的对象没有真棒的方法.
由于我只能控制我的课程,而是控制其他代码,我不能简单地决定只使用各种词法文件句柄.
有没有什么办法可以强制Input-> awesome永远是一个方法调用输入包,但从来没有一个文件句柄(至少在我控制的范围)?我认为不应该有任何名称冲突,因为输入包实际上是%Input :: stash.
完整的代码重现问题(另见这ideone):
use Strict; use warnings; use feature 'say'; say "This is perl $^V"; package Input { sub awesome { say "yay,this works"; } } # this works 'Input'->awesome; # the "open" is parsed,but not actually executed eval <<'END'; sub red_herring { open Input,\*STDIN or die $!; } END say "eval failed: $@" if $@; # this will die eval { 'Input'->awesome; }; say "Caught: $@" if $@;
示例输出:
This is perl v5.16.2 yay,this works Caught: Can't locate object method "awesome" via package "IO::File" at prog.pl line 27.
package My1; use warnings; use Strict; sub new { bless [],shift } sub awesome { 'My1'->new } __PACKAGE__@H_206_4@my2.pm
package My2; use warnings; use Strict; use parent 'My1'; sub try { my $self = shift; return ('My1'->awesome,$self->awesomE); } __PACKAGE__
script.pl
#!/usr/bin/perl use warnings; use Strict; use My2; open My1,'<&',*STDIN; my $o = 'My2'->new; print $o->awesome,$o->try;
以上是大佬教程为你收集整理的perl – 防止字符串被解释为文件句柄全部内容,希望文章能够帮你解决perl – 防止字符串被解释为文件句柄所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。