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:
- ( ) create a list,
- { } create a hash,
- [ ] create an array.
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 | [reply] [d/l] [select] |
| [reply] |
the "=>" operator does one thing more than the "," operator:
"he" automagically quotes the left argument, for you being able to "say":
my %hihohash = (hiho => "hiho");
| [reply] [d/l] |
When defining a normal array or hash, always use (). That's given, and hopefully easiest to remember since it's used the most.
To remember the braces for references, I use this logic:
# to access a member of an ARRAY, use []
$array[0] = "hello";
# so to define an anonymous array reference, use [] as well
$array_ref = [ "Hello", "World" ];
# to access a member of a HASH, use {}
$hash{'foo'} = "bar";
# so to define an anonymous hash reference, use {} as well
$hash_ref = { foo => 'bar', xyz => 'abc' };
So just remember how you access elements in an array/hash, and that will tell you which braces to use for anonymous refs as well. It won't take long for it to become second nature.
blokhead | [reply] [d/l] |
I wouldn't say there was a trick to remembering them, it's just a matter of remembering.
Either learn them or just use (), and if you need a reference, reference the hash or array you created with a backslash.
When the time comes, and you can't be bothered doing that any more then start using {} or [ ] as desired.
I know when I started writing Perl I didn't use {} or [ ] much, but used plenty of \% or \@.
Jasper | [reply] |
%hash = (foo => 'foo', bar => 'bar');
$href = \%hash;
And not:
$href = \(foo => 'foo', bar => 'bar');
Because the latter won't do what you might think. (It would be the same as:
$href = (\'foo', \'foo', \'bar', \'bar'); # which reduces to...
$href = \'bar';
which is almost certainly not what you want.)
bbfu
Black flowers blossum
Fearless on my breath | [reply] [d/l] [select] |
Perhaps this would help.
Pointy brackets, that is [and {,
go with references, scalars that point to thingies. | [reply] [d/l] [select] |
| [reply] |
| [reply] [d/l] [select] |