strat has asked for the wisdom of the Perl Monks concerning the following question:
Dear monks,
I just had a look at the function first in the Module List::Util and found the following code:
sub first (&@) { my $code = shift; foreach (@_) { return $_ if &{$code}(); } undef; # <====== BUG? }
In my eyes, the undef; should be replaced by a plain return; to prevent getting a (true!) list with one value (of undef), which in scalar context is true, e.g.
D:\workspace> perl D:\eclipse\workspace\BDC\lib> perl use strict; use warnings; use Data::Dumper; use List::Util; my @list = 1..20; my @first = List::Util::first { $_ == 21 } @list; print Dumper( \@first ); if( @first ) { print "In scalar-context: true\n"; } ^D $VAR1 = [ undef ]; In scalar-context: true
D:\workspace>perl -MList::Util -le "print $List::Util::VERSION" 1.19
Well, in the POD it is clearly stated that the first element is returned or undef - if not found. But since the syntax looks very similar to map and grep and the like, it might cause trouble without need if someone uses a list instead of a scalar to accept the result.
What do you think about?
Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: List::Util - Bug in first?
by kyle (Abbot) on Feb 18, 2008 at 14:14 UTC | |
Re: List::Util - Bug in first? (odds)
by tye (Sage) on Feb 18, 2008 at 16:12 UTC | |
Re: List::Util - Bug in first?
by ikegami (Patriarch) on Feb 18, 2008 at 16:22 UTC | |
by strat (Canon) on Feb 19, 2008 at 07:30 UTC |