Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Strange behavior with List::Util qw{first} on perl >= 5.20

by papioko (Initiate)
on May 26, 2015 at 20:18 UTC ( #1127889=perlquestion: print w/replies, xml ) Need Help??
papioko has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks Today when a try to run some of my work code on perl 5.20 (before that i used 5.18) i encountered a strange behavior that i can't understand. The Sample code is:
#!/usr/bin/env perl use strict; use warnings; use List::Util qw{ first }; my %hash = ( ASD => 1, ZXC => 2, QWE => 3, TYU => 4 ); my $val = "qwe"; if(my $res = first { lc $_ eq $val } keys %hash) { print "Found: $res\n"; } else { print "Not found\n"; };
On perl version prior 5.20 result will be "Found: QWE" (as i expected), but when i run this code on perl 5.20 and beyound, result will be "Found: qwe". What is that ? Bug? Or i missed something ?

Replies are listed 'Best First'.
Re: Strange behavior with List::Util qw{first} on perl >= 5.20
by Anonymous Monk on May 26, 2015 at 21:41 UTC

    Hmmmm... Perl v5.20:

    $ perl -MList::Util=first -le 'print first {lc eq "abc"} keys %{{ABC=>"ABC"}}' abc $ perl -MList::Util=first -le 'print first {uc eq "ABC"} keys %{{ABC=>"ABC"}}' ABC $ perl -MList::Util=first -le 'print first {/abc/i} keys %{{ABC=>"ABC"}}' ABC $ perl -MList::Util=first -le 'print first {lc eq "abc"} values %{{ABC=>"ABC"}}' ABC $ perl -MList::Util=first -le 'print first {lc eq "abc"} "ABC"' ABC $ perl -MList::Util=first -le 'print first {lc eq "abc"} values @{["ABC"]}' ABC

    On v5.10 they all (except for the last one) give "ABC". The inverse case, first {uc eq "ABC"} keys %{{abc=>"abc"}}, also gives the "unexpected" result "ABC". Smells like a bug... could this be rt://96343?

      A git bisect appears to confirm that. The bug also still appears to be present in the latest v5.22.0-RC2.

        There's something fishy about $_. It's a TEMP with REFCNT=1. I believe that means it's a mortal. If so, that means noone claims a reference to $_, but first still uses it after the callback returns. This points to a bug in List::Util.

        That said, the inplace lc($_)[1] should probably only happen in void context.


        1. This is the first I've ever heard about this!
Re: Strange behavior with List::Util qw{first} on perl >= 5.20
by dmitri (Priest) on May 26, 2015 at 21:09 UTC
    The hash does seem to have something to do with it. Strawberry Perl 5.20.0:
    C:\Users\>perl -e "use List::Util qw(first); my $f = first { lc $_ eq +'qwe' } qw(QWE); print $f" QWE C:\Users\>perl -e "use List::Util qw(first); %h = qw(QWE 1); my $f = f +irst { lc $_ eq 'qwe' } keys %h; print $f" qwe
Re: Strange behavior with List::Util qw{first} version 1.38
by Anonymous Monk on May 26, 2015 at 21:34 UTC
    Which version of List::Util? Upgrade List::Util? Try lc "$_"? What do you get with plain grep?
    $ perl -MData::Dump -e "%f = qw/ A A B B C C/; dd( grep { lc $_ eq q/c +/ } keys %f ); dd(\%f)" "C" { A => "A", B => "B", C => "C" }

    What do you get with non-hash?

    $ perl -E"say grep { lc $_ eq q/c/ } qw/ A B C/" C $ perl -MList::Util=first -E"say first { lc $_ eq q/c/ } qw/ A B C/" C
Re: Strange behavior with List::Util qw{first} on perl >= 5.20
by GotToBTru (Prior) on May 26, 2015 at 20:33 UTC

    Looks like it is first() in List::Util that is the cause; the hash is not being modified.

    Dum Spiro Spero
Re: Strange behavior with List::Util qw{first} on perl >= 5.20
by Anonymous Monk on May 26, 2015 at 21:23 UTC

    Perhaps a bug in ListUtil.xs — under debugger, correct output is produced.

Re: Strange behavior with List::Util qw{first} on perl >= 5.20
by Anonymous Monk on May 27, 2015 at 22:02 UTC
Re: Strange behavior with List::Util qw{first} on perl >= 5.20
by larryl (Scribe) on May 28, 2015 at 16:28 UTC
    https://rt.cpan.org/Public/Bug/Display.html?id=96343

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1127889]
Approved by hdb
Front-paged by GotToBTru
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2018-07-18 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (383 votes). Check out past polls.

    Notices?