in reply to Re^3: Using a sting with a variable name
in thread Using a sting with a variable name
I'll answer that myself:
*foo{THING} returns undef if that particular THING hasn't been used yet, except in the case of scalars. *foo{SCALAR} returns a reference to an anonymous scalar if $foo hasn't been used yet. This might change in a future release.And let me point out that this is from the perlref man page. It shows how to access "global" (package-based) variables through the symbol table.
For the OP and others, pardon me while I give some references to things in various posts on the thread. Follow up the presentation with the documentation of those concepts.
Farther down on that page is "symbolic references", which are also useful in this situation. That is, given a string $s that contains "var1" and use $$s to refer to $var1. You can't tell the difference between the variable existing but holding undef, or not previously existing. That's the general problem here. But, if your variables aren't undef, you might not have to worry about that.
It also shows ${"${pack}::$name"} = 5; which doesn't use eval. However, the use of eval in another post wasn't really to access it, but to get a compile-time error if a variable was used without being defined.
But... I agree that you should not do that. These tricks are for meta-programming, to write such things as package importers. For normal code, don't use a namespace as a hash.
—John update: picky nomenclature.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Using a sting with a variable name
by ikegami (Patriarch) on May 11, 2009 at 17:15 UTC |