You need to tread carefully around a couple of issues:
autovivification... if you ask defined(${'foo::bar::cantona'}) then the symbol table %foo::bar:: will pop into existence, if it does not already exist -- the variable $foo::bar::cantona, however, does not.
it is not always possible to tell the difference between $foo::bar::cantona not existing, and existing but being undef
The following avoids the autovivication issue, by starting in the root symbol table (%main::) and proceeding towards the full name:
this takes the full name of a symbol (e.g. 'foo::bar::cantona')returns 0 if the symbol table doesn't exist, or no symbol of the given name exists in the symbol table. It returns 2 if the name exists, but 3 if the name exists and the related scalar value is defined.sub edef { my ($symbol) = @_ ; no strict 'refs' ; my @path = split('::', $symbol) ; my $name = pop @path ; if (@path == 0) { @path = split('::', (caller)[0]) ; # use caller's package if no +ne given } ; if (($path[0] eq '') || ($path[0] eq 'main')) { shift @path ; # '::' => 'main::' and we start at 'main::' any +way } ; my $r_table = \%main:: ; foreach my $part (@path) { $part .= '::' ; if (!exists($r_table->{$part})) { return 0 ; } ; $r_table = $r_table->{$part} ; } ; if (!exists($r_table->{$name})) { return 0 ; } ; return defined(${$symbol}) + 2 ; } ;
Note that if (say) @foo::bar::cantona exists, then 2 is returned if $foo::bar::cantona does not exist, and also if it exists but is undef. I know of no way around this ambiguity.
In reply to Re: flavors of "defined"
by gone2015
in thread flavors of "defined"
by Wiggins
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |