in reply to beginner question - why is hash key unquoted and in capitals

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.

  • Comment on Re: beginner question - why is hash key unquoted and in capitals

Replies are listed 'Best First'.
Re^2: beginner question - why is hash key unquoted and in capitals
by morgon (Priest) on Oct 15, 2010 at 16:40 UTC
    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.

        btw:

        You should not alter postings in such a way that the original version (especially the parts that subsequent posts refer to) cannot be recovered anymore.

        Doing that leaves discusson threads that don't make any sense when viewed tomorrow.

        If you want to correct mistakes do it like this:

        I never make mistakes.

Re^2: beginner question - why is hash key unquoted and in capitals
by Anonymous Monk on Oct 15, 2010 at 20:42 UTC
    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.

        Hi, thanks for answering. That is what I thought but the other comment confused me (and a beginner is easily confused!)