Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

  (email not shown publicly)
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Wednesday September 10, 2003
11:04 PM

map bug

[ #14643 ]

This is a bug:

map { "$_" => "$_" } @array

Not only is that a bug, I know how to fix it and I won't have a problem with that again. Unfortunately, when I encountered this, it was strange and mysterious behavior to me. I searched for an explanation. I searched Perlmonks. I Googled. I boggled my fellow programmers. I boggled myself.

The answer, of course, was in perldoc -f map. I should have known better.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • I wouldn't call it a bug, it just isn't terribly useful. ;-)

    So what does the 'bugfix' look like?
    • Did you try it? It gives a syntax error, presumably because perl thinks the braces are indicating an anonymous hashref (which would need a following comma) rather than a block. Getting rid of the quotes around the first $_ is one way to fix it. Maybe it's not a bug (it is more or less documented in perlfunc), but the rules for the syntax certainly aren't obvious.
    • There are plenty of ways to fix it. Probably the clearest is to wrap the

      "$_" => "$_"

      in parentheses.

      map { ("$_" => "$_") } @array;

      In this case, it doesn't look terribly useful because it's a simplified test case. I discovered this when I and another programmer were being forced to walk symbol tables and pull out the CODE slot of blobs and examine them. I think posting the code we used to do that would have confused things even more :)

      As it stands, better examples are in perldoc and they sh

      • Oh! So I guess this one would have got me as well. It isn't always quite transparent to me when perl considers a pair of curlies constructors for an array-ref or rather for a code-block.