SPUG: Interesting bug using 'map'

Mark Mertel mark.mertel at yahoo.com
Fri Apr 10 11:30:53 PDT 2009

instead of using map, you could use a hash slice:

my ($fee, $fi, $fo) = @{ $fropper || $gropper }{ qw/fee fi fo/ };

Mark Mertel
mark.mertel at yahoo.com

From: "seasprocket at gmail.com" <seasprocket at gmail.com>
To: spug-list at pm.org
Sent: Thursday, April 9, 2009 8:52:22 PM
Subject: SPUG: Interesting bug using 'map'

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 string):

   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/20090410/0e1c1ff6/attachment.html>

More information about the spug-list mailing list