mbethke has asked for the wisdom of the Perl Monks concerning the following question:
Hi guys,
I just noticed that the given/when construct's way of introducing a lexical $_ plays badly with functions that take BLOCKs such as List::MoreUtils::any. The following does not print anything:
use 5.010; use strict; use warnings; use List::MoreUtils qw/ any /; my $s = "foo"; my @list = ( 1, undef, 2 ); given($s) { when('foo') { say "found undef" if any { ! defined($_) } @list; } }
Within the block, $_ is always 'foo' as it was bound during the block's definition.
Now this must be a known problem, right? For my inner peace, could someone explain me the rationale for making "given" so subtly different from say "for"? And for my productivity, is there a way around it short of "don't use 'given'"?
Thanks very much!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Lexical $_ in given/when vs. BLOCK arguments
by moritz (Cardinal) on Oct 25, 2011 at 10:02 UTC | |
by mbethke (Hermit) on Oct 25, 2011 at 18:06 UTC | |
by BrowserUk (Patriarch) on Oct 25, 2011 at 18:21 UTC | |
|
Re: Lexical $_ in given/when vs. BLOCK arguments
by ikegami (Patriarch) on Oct 25, 2011 at 17:42 UTC | |
|
Re: Lexical $_ in given/when vs. BLOCK arguments
by Anonymous Monk on Oct 25, 2011 at 09:52 UTC | |
by Anonymous Monk on Oct 25, 2011 at 10:00 UTC | |
by Anonymous Monk on Oct 25, 2011 at 10:17 UTC | |
by Anonymous Monk on Oct 25, 2011 at 10:03 UTC |