I don't know if the question was edited, but at this moment the question says "Why do we have parentheses around FOO/BAR in the hash?".

Hash assignment takes a list. The following shows that a lack of parentheses makes a difference:

use strict; use warnings; use constant FOO => 10; use constant BAR => 20; use Data::Dump qw/ dump /; my %BAZ = ( FOO() => 1, BAR() => 1 ); my %BOO = ( FOO => 1, BAR => 1 ); my %BAH = FOO => 1, BAR => 1; print 'BAZ: ' . dump(\%BAZ) . "\n"; print 'BOO: ' . dump(\%BOO) . "\n"; print 'BAH: ' . dump(\%BAH) . "\n";

This prints:

Useless use of a constant (BAR) in void context at temp/hash_test.pl line 17.
Odd number of elements in hash assignment at temp/hash_test.pl line 17.
BAZ: { 10 => 1, 20 => 1 }
BOO: { BAR => 1, FOO => 1 }
BAH: { FOO => undef }

Both comma operators (i.e. " =>" and " ,") have lower precedence than the assignment operator " =". Thus the assignment operator takes only the first argument to its right, which for the hash %BAH is the value for the constant FOO. It doesn't even "see" anything to the right of the first fat comma  => or even the comma itself.

Using parentheses around everything to the right of the assignment operator creates a list that the assignment operator in turn uses to create the hash.

Perl is an "Operator-oriented language" as chromatic mentions in his blog post Perl and the Least Surprised.


In reply to Re: understanding this syntax by molecules
in thread understanding this syntax by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.