Eva*_*oll 29 perl operators global-state flip-flop
我很沮丧.好的,所以这可能是我见过的最有趣的 Perl bug.即使在今天,我也在学习关于Perl的新内容.从本质上讲,触发器操作符..返回假,直到左手侧返回真,然后真,直到右手侧返回假保持全局状态(或者说是我承担.)
我可以重置它(也许这对Perl 4-esque几乎没有使用过是一个很好的补充reset())?或者,没有办法安全地使用此操作员?
我也没有看到这个(全局上下文位)记录在哪里perldoc perlop这是一个错误?
use feature ':5.10';
use strict;
use warnings;
sub search {
my $arr = shift;
grep { !( /start/ .. /never_exist/ ) } @$arr;
}
my @foo = qw/foo bar start baz end quz quz/;
my @bar = qw/foo bar start baz end quz quz/;
say 'first shot - foo';
say for search \@foo;
say 'second shot - bar';
say for search \@bar;
Run Code Online (Sandbox Code Playgroud)
$ perl test.pl
first shot
foo
bar
second shot
Run Code Online (Sandbox Code Playgroud)
yst*_*sth 34
有人可以澄清文档的问题是什么吗?它清楚地表明:
Each ".." operator maintains its own boolean state.
Run Code Online (Sandbox Code Playgroud)
关于"每个"的含义有一些含糊不清的含义,但我不认为复杂的解释可以很好地满足文档的要求.
请注意,Perl的其他迭代器(each或标量上下文glob)可能会导致相同的问题.因为状态for each绑定到特定的散列,而不是特定的代码位,each可以通过调用(甚至在void上下文中)keys散列来重置.但是对于globor ..,除了通过调用迭代器直到它被重置之外,没有可用的重置机制.一个示例glob bug:
sub globme {
print "globbing $_[0]:\n";
print "got: ".glob("{$_[0]}")."\n" for 1..2;
}
globme("a,b,c");
globme("d,e,f");
__END__
globbing a,b,c:
got: a
got: b
globbing d,e,f:
got: c
Use of uninitialized value in concatenation (.) or string at - line 3.
got:
Run Code Online (Sandbox Code Playgroud)
对于过于好奇,这里有一些例子,其中相同..在源中是一个不同的 ..运算符:
单独关闭:
sub make_closure {
my $x;
return sub {
$x if 0; # Look, ma, I'm a closure
scalar( $^O..!$^O ); # handy values of true..false that don't trigger ..'s implicit comparison to $.
}
}
print make_closure()->(), make_closure()->();
__END__
11
Run Code Online (Sandbox Code Playgroud)
注释掉这一$x if 0行,看看非闭包有一个单独的..操作,由所有"副本"共享,输出为12.
主题:
use threads;
sub coderef { sub { scalar( $^O..!$^O ) } }
coderef()->();
print threads->create( coderef() )->join(), threads->create( coderef() )->join();
__END__
22
Run Code Online (Sandbox Code Playgroud)
线程代码以线程创建之前的任何状态开始,但是线程中对其状态的更改被隔离而不会影响其他任何内容.
递归:
sub flopme {
my $recurse = $_[0];
flopme($recurse-1) if $recurse;
print " "x$recurse, scalar( $^O..!$^O ), "\n";
flopme($recurse-1) if $recurse;
}
flopme(2)
__END__
1
1
2
1
3
2
4
Run Code Online (Sandbox Code Playgroud)
每个递归深度都是一个单独的..运算符.
bri*_*foy 18
诀窍不是使用相同的触发器,所以你没有担心的状态.只需制作一个生成器函数,为您提供一个新的子程序,只需使用一次新的触发器:
sub make_search {
my( $left, $right ) = @_;
sub {
grep { !( /\Q$left\E/ .. /\Q$right\E/ ) } @{$_[0]};
}
}
my $search_sub1 = make_search( 'start', 'never_existed' );
my $search_sub2 = make_search( 'start', 'never_existed' );
my @foo = qw/foo bar start baz end quz quz/;
my $count1 = $search_sub1->( \@foo );
my $count2 = $search_sub2->( \@foo );
print "count1 $count1 and count2 $count2\n";
Run Code Online (Sandbox Code Playgroud)
我还在Make独有的触发器操作器中写了这个.
"范围算子" ..记录在"范围操作符"下的perlop中.通过显示,看起来没有任何方法可以重置..运算符的状态...运算符的每个实例都保持自己的状态,这意味着没有任何方法可以引用任何特定..运算符的状态.
它看起来像是为非常小的脚本设计的,例如:
if (101 .. 200) { print; }
Run Code Online (Sandbox Code Playgroud)
文档说明这是简称
if ($. == 101 .. $. == 200) { print; }
Run Code Online (Sandbox Code Playgroud)
不知何故,使用$.是隐含的(工具在评论中指出,这也是有记录的).这个想法似乎是这个循环在Perl解释器的给定实例中运行一次(直到$. == 200),因此您不必担心重置..触发器的状态.
由于您已确定的原因,此运算符在更通用的可重用上下文中似乎不太有用.
针对您的特定情况的解决方法/黑客/作弊是将最终值附加到您的数组:
sub search {
my $arr = shift;
grep { !( /start/ .. /never_exist/ ) } @$arr, 'never_exist';
}
Run Code Online (Sandbox Code Playgroud)
这将保证范围运营商的RHS最终成立.
当然,这绝不是一般解决方案.
在我看来,这种行为没有明确记录.如果您可以构建清晰的解释,则可以将补丁应用于perlop.podvia perlbug.