SPUG: Interesting bug using 'map'

seasprocket at gmail.com seasprocket at gmail.com
Thu Apr 9 20:52:22 PDT 2009

I ran into a bug tonight using 'map' which taught me something about map and
raised a further question:

   my ($fee, $fi, $fo) =
       map {$fropper ? $fropper->$_ : $glopper->$_}
           qw/fee fi fo/;

The bug arises bc fropper and glopper are not guaranteed to have return
values for fee/fi/fo, and bc map evaluates BLOCK in LIST context. So if fee
or fi is undefined, you get an empty list which collapses in the final
result. Then fi or fo can get assigned to the wrong variable.

Here's my question, I can prevent it this way (by ORing with an empty

   my ($fee, $fi, $fo) =
       map {$fropper ? $fropper->$_ || '' : $glopper->$_ || ''}
           qw/fee fi fo/;

But that's ugly -- is there a cleaner way to avoid this problem?

2People citizen's network for climate action: http://www.2people.org

Greater Seattle Climate Dialogues: http://www.climatedialogues.org
The Great Warming coalition
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/spug-list/attachments/20090409/26d7eb5b/attachment.html>

More information about the spug-list mailing list