develooper Front page | perl.perl5.porters | Postings from June 2008

Re: How to dig through symbol tables

Thread Previous | Thread Next
From:
Ben Morrow
Date:
June 19, 2008 18:18
Subject:
Re: How to dig through symbol tables
Message ID:
3lcri5-0jb1.ln1@osiris.mauzo.dyndns.org

Quoth sorourke@ucsd.edu:
> I'm trying to figure out the "right" way to dig through a
> program's symbol table without disrupting it.  According to the
> docs, "defined" only works for scalars and subs.  I would think
> that "defined *{FOO}{THING}" would work for everything, but it
> exhibits this annoying behavior:
> 
>     $ perl -le 'sub x{};for (qw(SCALAR ARRAY HASH CODE IO)) { print
> "foo-thing $_" if defined *{x}{$_}; }'
>     foo-thing SCALAR
>     foo-thing CODE
> 
> Indeed, if any other slot is defined, *{FOO}{SCALAR} seems to be
> defined.  Is this intended behavior?

Yes. If a glob exists at all it has a SCALAR slot.

> I am currently using *{FOO}{THING} for everything but scalars,
> and (afterwards) "defined $FOO" for scalars, but it seems like
> there should be a better way.

'defined $FOO' tells you whether the scalar contains undef or not, not
whether it exists. For instance

    our $FOO;
    defined $FOO;

returns false, even though $FOO exists. Basically, if any part of *FOO
exists, $FOO exists as well.

Ben

-- 
#!/bin/sh
quine="echo 'eval \$quine' >> \$0; echo quined"
eval $quine
#                                                        [ben@morrow.me.uk]

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About