Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: map problem

by morgon (Priest)
on Mar 09, 2012 at 12:59 UTC ( [id://958701]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://958701]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-25 08:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found