[Melbourne-pm] Nested maps returning hash references

Toby Corkindale toby.corkindale at strategicdata.com.au
Wed Oct 5 17:45:15 PDT 2011


On 05/10/11 21:03, Myf White wrote:
> Alfie,
>
> The + was missing in your previous email. I put it in here and it worked
> as advertised:
> map {
>       my $f = $_;
>       +{     #<<<<<<<<<<<<<<<<<<--- HERE
>            map {
>                 $f->can($_) ? ( $_ => $f->$_() ) : ()
>            } @fields
>       }
> } @foos

This is weird.. I still get an error when I do that.
This code generates "syntax error at ./maptest.pl line 15, near "}""
.. aah, I need to lose the semi-colon on the inner map.

my $foo = [
     map {
        my $f = $_;
        +{  <--- plus added here
            map {
                $f->can($_) ? ($_ => $f->$_) : ()
            } qw(label name html); <-- semi-colon must die
        }
    } @fields
];




I agree, the problem was Perl interpreting the braces as a block rather 
than a hash reference. Kind of annoying that it only uses a heuristic!
Oh well, probably all my fault for trying to nest the damn things when I 
should have been jumping out to subroutines.. but you know how it goes, 
sometimes you just want to try it to see if it'll work :)


More information about the Melbourne-pm mailing list