Dear monks,

I wrote this bug last week. It's not a particularly nasty bug, but it looks so innocent I thought I'd share, particularly for the benefit of brothers and sisters who are getting the hang or using map.

In essence, the code was meant to produce an array of numbers from an array of objects, like this:

my @row = map $_->value, @$arr;
But some of the elements in @$arr could be undefined, and of those that were defined, some could have the string 'NA' or undef returned by the value method. I wanted all these cases to show up as undef in the LHS, so I wrote something like this:
for my $arr ( @results ) { ... my @row = map val( $_ ), @$arr; ... } sub val { my $o = shift; if ( defined $o ) { my $v = $o->value; return $v if defined $v and $v ne 'NA'; } return; }
Spot the bug? It's in the second return statement of val. Without an argument, it causes val to return undef in scalar context, but return the empty list in list context. As it happens, map evaluates its first argument in list context. Bottom line: even though, in every iteration, the size of @$arr was the same, the size of @row would sometimes be smaller.

Here's a simple illustration of the same thing:

sub foo { if ( $_[ 0 ] % 3 } { return $_[ 0 ]; # argument divisible by 3 } return; } print join( ':', map foo( $_ ), 1..5 ), "\n"; __END__ 1:2:4:5
Note that the output has 4 elements, while the input had 5. If one wants those undefs in the output from map, one either has to force the scalar context
print join( ':', map scalar foo( $_ ), 1..5 ), "\n"; __END__ 1:2::4:5
or change the last statement in foo to an explicit return undef; . I'd pick the former approach for flexibility, the latter one for clarity.

It is worth noting, however, that, contrary to map, grep evaluates its first argument in scalar context:

print join( ' ', grep sub { wantarray ? 0 : 1 }, 1..3 ), "\n"; __END__ 1 2 3

Update: Twice I referred to "second argument" (of map/grep) when I meant "first argument". I'm still scratching my head over the origins of this braino, but thanks to itub for pointing it out.

the lowliest monk


In reply to Unhappy returns by tlm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.