> 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"

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

>
> 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.

> >
> > 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.

as noted in comment on Blogs.perl.org
Here's a work-around. A bit ugly but ...
when(1) { say $_ if any {our $_; $_ == 2} (2,3)}
The Anonymous function can specify that it's not a closure over the
implicit lexical $_ of given but rather is using global $_ by declaring
it our $_;.
Maybe we need to propose doc fixes to the impacted List::* modules' POD
to give this workaround.
[ The alternative I can think of is for the any() etc functions to be
made magic like grep. Maybe a B::* using utility subr could munge the
optree to switch lexical $_ refs to global in the block arg. Would want
it to only do that once for any given block. Would be easier with Perl6
AST edits! ]