Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

what's wrong with that syntax construction?

by basiliscos (Pilgrim)
on Jun 15, 2016 at 13:16 UTC ( [id://1165723]=perlquestion: print w/replies, xml ) Need Help??

basiliscos has asked for the wisdom of the Perl Monks concerning the following question:

I would like to generate random hash, i.e. where key is random number and value is also random number

my @x = map { rand => rand } (0 .. 5) Compile error: syntax error at (eval 310) line 5, near "} (" BEGIN not safe after errors--compilation aborted at (eval 310) line 5.

Mean while, the following works:

my @x = map { rand() => rand } (0 .. 5);
WBR, basiliscos.

Replies are listed 'Best First'.
Re: what's wrong with that syntax construction?
by BrowserUk (Patriarch) on Jun 15, 2016 at 13:31 UTC

    Without the parens, the first 'rand' is taken as a string; and that causes the parser to see { 'rand' => rand } as an anomymous hash.

    With the parens, it parses { rand() => rand } as an anonymous block as you intend.

    You can also force the parser to see the anonymous block like this: my @x = map {; rand => rand } (0 .. 5);.

    The extra semicolon is legal inside an anonymous block, but not in an anonymous hash, so the parser gives the benefit of the doubt and goes the way you intend.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice. Not understood.
Re: what's wrong with that syntax construction?
by Athanasius (Archbishop) on Jun 15, 2016 at 13:31 UTC

    Hello basiliscos,

    The “fat comma” operator, =>, stringifies its left-hand operand, so the Perl compiler sees a string and not a function, i.e. my @x = map { 'rand', rand } (0 .. 5). But with parentheses, the expression rand() is evaluated before the => operator, so it is the function’s return value that is stringified.

    See perlop#Comma-Operator.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: what's wrong with that syntax construction?
by Eily (Monsignor) on Jun 15, 2016 at 13:33 UTC

    In this case it's a good thing you get a syntax error because if it did compile it wouldn't do what you want. The fat comma (=>) forces its left side to be interpreted as a bareword, so a string most of the time*. So the left side of rand => rand is "rand". Somehow, seeing something that can only be a bareword after the { confuses perl into parsing the construct incorrectly.

    * print rand =>; is parsed as print(rand, $_);, not print(rand()); and not print("rand"); either.

    Edit: as pointed out by BrowserUK and Athanasius, here the bareword is interpreted as a string, which makes the code look like an anonymous hashref.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-19 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found