in reply to map problem

That's because when Perl sees the { following the map, it has to guess whether it's seeing the beginning of a block, or the beginning of a hashref, and it may only keep one token ahead. Which is a string, so map guesses hashref, so, it assumes it's going to parse map EXPR, LIST. And then there's no comma.

Use:

my %y = map {;"prefix_$_" => 1 } qw(a b c);
The semi-colon tells Perl it's a block.

Replies are listed 'Best First'.
Re^2: map problem
by morgon (Priest) on Mar 09, 2012 at 12:59 UTC
    In my particular case I would prefer this syntax:
    my %y = map { ("x$_" => 1) } qw(a b c);
    or would you say that using the semicolon is already an accepted idiom for such cases?
      I'm not the only one using semi-colons. I think I picked this up from Larry Rossler (the one from the GRT) back in the 1990s.

      But your use of parenthesis still makes it ambiguous. Perl may now guess right, but it's still a guess. {("x$_" => 1)} can still be a hashref. Perl will guess incorrectly if it's written as:

      my @y = map { ("x$_" => 1) }, qw(a b c);
      But feel free to do whatever works for you. Don't do something just because others do it, or not do something because others don't.

      Or you could do it like this:

      my %y = map( ( "x$_" => 1 ), qw( a b c ) );

      The code with the semi-colon

      • has an unambiguous effect (not a valid hash constructor),
      • has an unambiguous purpose (coder obviously doing something to appease the compiler), and
      • is idiomatic.

      { ("x$_" => 1) } is still a valid hash constructor, and it's not clear that the parens are required, so it's not as clear as it could be.