in reply to Re: map problem
in thread map problem

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?

Replies are listed 'Best First'.
Re^3: map problem
by JavaFan (Canon) on Mar 09, 2012 at 14:12 UTC
    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.
Re^3: map problem
by jwkrahn (Abbot) on Mar 09, 2012 at 13:14 UTC

    Or you could do it like this:

    my %y = map( ( "x$_" => 1 ), qw( a b c ) );
Re^3: map problem
by ikegami (Patriarch) on Mar 09, 2012 at 22:34 UTC

    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.