in reply to (5.6.1) map mayhem?

When Perl sees an open brace ({) that isn't part of some construct (like $hash{$key}), it sometimes isn't sure whether it is meant to start a block or an anonymous hash reference. You can tell Perl which you meant by using +{ for anonymous hash reference and {; for a block.

You are trying to pass a block to map so you can change the problematic line to:

$two->( map {; anda => $_ } @one );
which is a bit ugly.

                - tye

Replies are listed 'Best First'.
Re: Re: (5.6.1) map mayhem? (+{ {;)
by bart (Canon) on Oct 07, 2003 at 21:26 UTC
    IMO Perl should always interpret an opening brace right after the map and grep keywords, as the start of a block. There's no excuse for it, preferring to treat it as an anonymous hash.

    Do you ever think you're seeing an anonymous hash in that position? I didn't think so.

      That might be the obvious case in hindsight. But you can't change the DWIMmery radically now, without having something subtly being made different. Also, I think it's the same DWIMmery used at the beginning of a statement that might also be a return value for a subroutine: you sometimes want that to be a block, and sometimes and hashref, hence the DWIMmery.

      Personally, I find the "+" prefix and ";" suffix to that open brace a bit tacky, but certainly a lot better than having a language force me to always use 8-space tabs just to make sure that lined up things stay lined up since the lineup is syntactically significant. {grin}

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        but certainly a lot better than having a language force me to always use 8-space tabs just to make sure that lined up things stay lined up since the lineup is syntactically significant.

        Not sure which language you are talking about, but I take Pythons or make's significant whitespace rules over the Perl significant whitespace rules (both of perl5 and perl6) any day, and twice on Wednesdays.

        At least the rules in Python and make make sense, those of Perl don't.

        Abigail

      If you're me you do. I rarely ever use blocks with map - if I've got braces then its going to be a hash (though the dwimmery forces me to prepend a '+' to make it behave).