develooper Front page | perl.perl5.porters | Postings from July 2009

Re: [perl #67694] List::Util attaching to the wrong $_ when usedinside given/when construct

Thread Previous
From:
Dave Mitchell
Date:
July 24, 2009 11:06
Subject:
Re: [perl #67694] List::Util attaching to the wrong $_ when usedinside given/when construct
Message ID:
20090724180552.GS4204@iabyn.com
On Fri, Jul 24, 2009 at 07:16:17PM +0200, Bram wrote:
> Citeren Dave Mitchell <davem@iabyn.com>:
>
>> On Tue, Jul 21, 2009 at 12:45:04PM +0200, Rafael Garcia-Suarez wrote:
>>> Ah, you're right. The parser has this line :
>>> switch  :       label GIVEN '(' remember mydefsv mexpr ')' mblock
>>> where mydefsv is an empty rule that lexicalizes $_.
>>> But then the code I pasted above could probably be simplified.
>>
>>
>> So if I've understood this correctly, given() adds a lexical $_ to the
>> scope, and there's a bug in List::Util::first in that it doesn't work with
>> a lexical $_? So not a bug in given/smartmatch?
>
> I'm not sure if 'bug' is the correct word.

Well,

    use List::Util  qw/first/;
    {
	my $_ = 0;
	print "ok grep \n" if grep  { $_ } 1;
	print "ok first\n" if first { $_ } 1;
    }

outputs

    ok grep 

So, the behaviour of first() could be described as unexpected, to say the
least. I think the correct behaviour of first() would be to to modify the
$_ in the caller's scope rather than what's currently in scope.

-- 
Nothing ventured, nothing lost.

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About