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

Since starting to code in Perl I have this problem with using curly-braces where it should be parens, and vice-versa. I would like to know what the thinking is behind the purpose of each brace so that I dont waste as much time.

Also, if you do not know the technical issues behind the ideologies of each character, but do have some personal way of remembering how to use the two properly, feel free to share those with me as well.

Some examples of common mistakes ( _NOT_ the only ones):

my %bad_hash = {foo => 'bar'}; my %good_hash = (fun => 'Simpsons' ); my $bad_ref = (this => 'that'); my $good_ref = {more_fun => 'Simpsons DVDs'};

the_Don
...making offers others can't rufuse.

Replies are listed 'Best First'.
Re: The CORRECT thinking behind the {}s and the ()s.
by VSarkiss (Monsignor) on Sep 16, 2002 at 16:35 UTC

    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

Re: The CORRECT thinking behind the {}s and the ()s.
by samurai (Monk) on Sep 16, 2002 at 16:22 UTC
    Curly braces denote anonymous hashes. So %bad_hash only gets one element, whose key is a hash_ref. The regular parenthesis denote an array, which is how you define a hash. $ary[0] is the first key, $ary[1] is the first values, $ary2 is the second key and so on and so forth. => is just a pretty alias to the comma operator.

    --
    perl: code of the samurai

      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");
Re: The CORRECT thinking behind the {}s and the ()s.
by blokhead (Monsignor) on Sep 16, 2002 at 19:27 UTC
    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

Re: The CORRECT thinking behind the {}s and the ()s.
by Jasper (Chaplain) on Sep 16, 2002 at 16:36 UTC
    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

      Just to confuse things more... :)

      Either learn them or just use (), and if you need a reference, reference the hash or array you created with a backslash. (em added)

      Just be careful that you remember that that means:

      %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

Re: The CORRECT thinking behind the {}s and the ()s.
by Anonymous Monk on Sep 17, 2002 at 16:01 UTC
    Perhaps this would help.

    Pointy brackets, that is  [and {,
    go with references, scalars that point to thingies.

      I can not speak for the rest of the monastery, but that is exactly what helps me. Thank you.

      the_Don
      ...making offers others can't rufuse.

        My pleasure. I had never thought of that until I read your question.

        I would also suggest use strict; and -w or use warnings; and
        coding and running small chunks of code often. This will find these and
        other errors and dubi-osities.

        Updated spelling error