bug in perl ?

TeeJay on 2003-05-16T13:37:36

      1 #!/usr/bin/perl
      2 use strict;
      3 use Data::Dumper;
      4 my @oldfields = qw/old_foo old_bar old_baz/;
      5 my @newfields = qw/foo bar baz/;
      6
      7 my $hashref = { old_foo => 'aaa', old_bar => 'bbb', old_baz => 'ccc' };
      8
      9 $hashref->{new_values} = { map { "new_$_" => $hashref->{"old_$_"} } @newfields };
     10 # $details->{company_address} = { map { "addr_$_" => $details->{"comp_addr_$_"} } qw/postal_address post_code country phone_no fax        _no mobile_no/ };
     11
     12 print "data structure : \n", Dumper(%$hashref), "\n";
seems to upset perls compiler (versions 5.6 and 5.8.0) which is a shame as I planned to munge a load of data this way.

perl gives the error message :
syntax error at hashrefslice.pl line 9, near "} @newfields "

richardc seems to agree somethings not right..

is there something we missed ? perhaps there is a more elegant way that works.


It works with more parens

koschei on 2003-05-16T13:51:09

map { ("new_$_" => $hashref->{"old_$_"}) } @newfields
Note the extra parens.

It's still weird though.

You've just confused the parse

Elian on 2003-05-16T14:20:40

The potential meaning of { is so overloaded that perl has to do some probabalistic analysis to figure out what you mean when you put a { in--is it the left bracket for a block, a hash index, or an anonymous hash constructor? (There's some interesting code that calculates a weighted score based on surrounding constructs, and decides based on that) All that's happened is that your code has, in a mildly ambiguous circumstance, gone down the wrong path. Dunno if I'd call this a bug in perl, since the offending line can certainly be interpreted in several ways.

Short answer: You're being Too Damn Clever and confused the parser. Stop that.

Re:You've just confused the parse

rafael on 2003-05-16T14:26:07

And that's even documented in perlfunc/map.

Re:You've just confused the parse

TeeJay on 2003-05-16T15:07:09

I wasn't sure it was a bug in perl - hence the journal entry..

Like I said - There had a to be an elegant way of doing it and in the end I just simplified it so that it no longer used "old_$_" but $_ and ItJustWorked(TM).