in reply to The CORRECT thinking behind the {}s and the ()s.
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:
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).my $hash = { fun => 'Simpsons' }
The simple, though inexact, way to remember is that if you're assiging to a hash or an 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.$hash = ... $array = ...
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
|
|---|