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. | [reply] [d/l] [select] |
|
|
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.
| [reply] [d/l] [select] |
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. | [reply] [d/l] |
Re: beginner question - why is hash key unquoted and in capitals
by toolic (Bishop) on Oct 15, 2010 at 16:02 UTC
|
| [reply] |
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.
| [reply] |
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. | [reply] |
|
|
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. | [reply] [d/l] |
|
|
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.
| |
|
|
| [reply] |
|
|
|
|
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
| [reply] |
|
|
| [reply] [d/l] [select] |
|
|