Yes and no.
It's not possible to get a reliable answer for scalar package variables, but you can get a close enough answer. (If pkg var @foo exists, so does $foo even if it's never used.)
It's not possible with lexical variables unless you're ok with actually executing the literal (and thus arbitrary code) to find out.
There are template systems that use the syntax of Perl's interpolating literals.
| [reply] [d/l] [select] |
Why is it not possible for scalar package variables, but possible for array package variables?
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.
| [reply] [d/l] |
| [reply] |
I agree this is a bad idea, but this should work:
#!/usr/local/bin/perl -w
use strict;
my $var2 = 1;
# may just be $var
my $one = '$var';
my $two = '$var2';
if(eval("$one")){
print "I don't want to see this\n"
}
if(eval("$two")){
print "I want to see this\n"
}
| [reply] [d/l] |
Let's hope you trust the template...
my $one = '$var[ system qw( rm -rf / ) ]';
Even the following would do with the method you used:
my $one = 'system qw( rm -rf / )';
| [reply] [d/l] [select] |
Sometimes you really need to know whether a variable is defined; wiping the hard drive every time you run the script is just the price of that knowledge. ;-)
--
use JAPH;
print JAPH::asString();
| [reply] |
I'm not going to use this method... I have actually implemented it using hashes. I mainly wondered if one wanted to do it how would they go abut doing it. Lots of replies! :-)
| [reply] |