You need to tread carefully around a couple of issues:

The following avoids the autovivication issue, by starting in the root symbol table (%main::) and proceeding towards the full name:

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 ; } ;
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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.