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

Hi This code is from a book I am learning from:
$sub{$dir}{CALLBACK} = $callback;
I can see that the value corresponding the key held in the $dir variable in the hash called sub is a reference to another hash and that we are dereferencing it and adding the value of the variable $callback to its CALLBACK key. But why is the key CALLBACK in captials and unquoted. I have not seen this before. Many thanks

Replies are listed 'Best First'.
Re: beginner question - why is hash key unquoted and in capitals
by morgon (Priest) on Oct 15, 2010 at 15:58 UTC
    This "CALLBACK" is a so-called bareword and in certain places (e.g. here when used as hash-key) barewords are implicitely quoted.

    So $hash{CODEREF} and $hash{"CODEREF"} are the same thing.

    Another typical usage would be

    my %hash = ( HUBBA => 1, BUBBA => 2 );
    Again the keys to the hash are implicitely quoted.

    However you have to be careful when you use non-chars in the key: $hash{HUBBA-BUBBA} will not work as the Perl-parser parses the key as an expression and you get an error (you would have to quote explicitely here: $hash{"HUBBA-BUBBA"}.

    The use of all-caps is only a matter of taste and has no significance.

      There is one more thing that may be worth knowing:

      While you cannot use $hash{HUBBA-BUBBA} (see earlier posting) is is perfectly legal (even under strict) to use e.g. $hash{-HUBBA}.

      The rationale for this is I believe the fact that options traditionally (in some systems) start with a "-".

      $hash{+HUBBA} however is illegal.

Re: beginner question - why is hash key unquoted and in capitals
by kennethk (Abbot) on Oct 15, 2010 at 15:59 UTC
    Splitting your question in twain,

    why is the key CALLBACK in captials

    That is purely a stylistic choice. Perl is case sensitive, so a key CALLBACK will point to a different value than callback or Callback. I have a colleague who always uses all caps in his hash keys. I think it's not great to look at, but his code works just fine.

    why is the key CALLBACK ... unquoted

    If a hash key starts with _ or an alphabetic character and contains only word characters (_, digits and letters), Perl with automatically stringify it for you. This means you do not need to quote hash keys in most cases, though the habit can bite you if you start using whitespace of punctuation. It is documented in Comma Operator in the discussion of the => operator.

Re: beginner question - why is hash key unquoted and in capitals
by toolic (Bishop) on Oct 15, 2010 at 16:02 UTC
    Form the free online Perl documentation (perldata):
    In fact, an identifier within such curlies is forced to be a string, as is any simple identifier within a hash subscript. Neither need quoting. Our earlier example, $days{'Feb'} can be written as $days{Feb} and the quotes will be assumed automatically. But anything more complicated in the subscript will be interpreted as an expression.

    Perl is case-sensitive. So are hash keys.

Re: beginner question - why is hash key unquoted and in capitals
by muba (Priest) on Oct 15, 2010 at 16:02 UTC

    Hash keys hardly ever have to be quoted, especially not if they're just one-word strings. As for the key being capitalized, that's just due to the author of that snippet adhering to one or another convention. Technically it doesn't really matter, as long as one is consistent throughout the entire program.

Re: beginner question - why is hash key unquoted and in capitals
by halfcountplus (Hermit) on Oct 15, 2010 at 16:03 UTC
    I can see that the value corresponding the key held in the $dir variable in the hash called sub is a reference to another hash and that we are dereferencing it

    Doesn't look that way to me. $dir should be a straight scalar variable. Nothing is being dereferenced with that syntax either.

    But why is the key CALLBACK in captials and unquoted.

    The caps are presumably just style. You can use string literals/barewords unquoted in perl if you run without use strict -- which general consensus at this point is there is no reason to not use strict. It helps. (You will get a warning if you use warnings anyway, about potential future reserved words.) HOWEVER, as per previous posts, hash keys are always okay.

      You can use string literals/barewords unquoted in perl (as per the previous posts) only if you run without use strict
      That is not true. Example:
      use strict; my %hash; $hash{HUBBA}= "it works!"; print $hash{HUBBA};
      Barewords as hash-keys are implicitely quoted even under strict.

        I have frequently seen code like:

        my %magic_words = { PLUGH => "A hollow voice says", FOO => "You are nothing but a charlatan", }

        Which is an obviously-intended use for “barewords.”   Nevertheless, I have gotten into the habit of enclosing all literals in single or double quotes.

        Correct -- all apologies, will change post.

      Hi If nothing is being dereferenced, can you tell me what is going on? Why are there 2 adjacent pairs of curly braces? I thought when you say that it meant there was an implicit -> operator? I am probably wrong and have only just read about references today. I understand the concept but find the notation confusing thanks
        $sub{$dir}{CALLBACK} is short hand for $sub{$dir}->{CALLBACK}, and they are literally equivalent. Any time an array or a hash contain array or hash references, you can use that notation. This is discussed in Arrow Rule in perlreftut.

        The value held in $sub{$dir} is being dereferenced as a hash, and then the value stored with key CALLBACK is being set to $callback. I find halfcountplus's statement on this misleading at best.