On Mon Jun 08 18:45:35 2009, paauull wrote:
Show quoted text> The given() and when() constructs added in Perl5.10 appear to mess with
> $_ such that List::MoreUtils::any(), and presumably the others (all(),
> none() and notall()) do not function correctly. This code is a snippet
> from the attached file. It prints "any() broken"
As well it is expected. See below.
Show quoted text> $val = 1;
> given( $val ){
> when(1) {
> say 'any() ' . (( any{ 14 == $_ } 10..15) ? 'works' : 'broken' );
> }
> }
given() creates a lexical variable, called $_, which is visible in the scope of the controlled block. This
is a lexical variable, not a global one. Because it is lexical, the $_ that appears in any()'s control
block sees this variable, and not the global one. any() can only see the global one, not the lexical one.
Consider it as if the code were written instead:
given( my $dollarsmudge = $val ) {
when(1) {
say 'any() ' . (( any{ 14 == $dollarsmudge } 10..15) ? 'works' : 'broken' );
}
}
This is fundamental to the way given/when work, and is not something that can be changed.
--
Paul Evans