$ref = \%hash;
$ref = \@hash;
How do I do the same thing as reference using typeglob in perl?
What’s the exact steps perl takes to interpret $$ref{key}?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you’re asking how you get a reference to a type glob, it’s just:
For a “literal name” of the symbol, (that is where you type in the exact name of the symbol), and not a variable. But, you can do this:
for a variable symbol. However, the above works with
strictand the easier one below doesn’t:One of the nice things about
Symbol::qualify*, is that it handles package names as the second variable. So…does the same thing as
\*{$some_other_package.'::'.$symbol_name}and it works withstrict.Once you have the symbol ref, to get the slot, you have to deference the reference, so perl does not think you’re trying to use it as a hash, like so.
Another Take
I put your two ideas together in a different way. If you have a symbol table reference, you can get the
HASHslot and that is a reference just like any other reference to a hash. So you can do the following.Either
Or
will work. These are safer, though
If you want to do this not with a direct entry, but a reference into the table, you would do the following:
NOTE:
%hashabsolutely needs to be a package variable. Only package variables reside in the symbol table (so only subs and@ISAand Exporter variables tend to be in modern symbol tables). Lexical variables (those declaredmy) reside in the “pad”.UPDATE:
I have gotten away from using
Symbolso much. Curiously, even though it is core, it seems non-standard in the way Perlers do–and see–things. Instead, I use the direct way in what I call “no-blocks”, as localized as I can make them.OR
I almost always use the
*STDERR{IO}idiom for a glob file handle reference. In modern Perl, these are usually objects.