It's true that parentheses are very overloaded in Perl, and do many things. In the specific cases you mention, though, it's not so bad:

So when you say:
my %hash = (fun => 'Simpsons')
You're creating a two element list, and assigning that to %hash. However, when you say:
my $hash = { fun => 'Simpsons' }
You're creating a two-element anonymous hash, and storing a reference to it in $hash (note the $ sigil, as opposed to the % sigil in the previous example).

The simple, though inexact, way to remember is that if you're assiging to a hash or an array:

%hash = ... @array = ...
You should have a list, which are surrounded by parens. But if you're assigning to a scalar:
$hash = ... $array = ...
You should have an anonymous array or an anonymous hash, which are surrounded by brackets or curlies.

This isn't exactly right. In fact, you can assign an anonymous hash as an element of another hash, which is what your %bad_hash = { ... } is doing. And assigning a list to a scalar isn't an error, but it won't do what your examples are trying to show. What I wrote is just a simple guideline to get you through until you're comfortable with the underlying concepts.

HTH


In reply to Re: The CORRECT thinking behind the {}s and the ()s. by VSarkiss
in thread The CORRECT thinking behind the {}s and the ()s. by the_Don

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.