Phoenix.pm: Symbol Tables

Shay Harding mekla at geocities.com
Sun Oct 3 22:15:31 CDT 1999


>I have a situation where I would like to know if a certain subroutine name 
>has already been defined.  Do any of you guys know how to check the symbol 
>tables for that?  I have tried a few typeglob solutions but none seem to 
>work.  I know that I can eval the code and then check the existence of error 
>messages ($@), but I'd rather not.
>
>I thought I found the answer on page 282 of the "Camel Book". But checking 
>the definedness of:
>    *main::load_email{CODE}
>did not test as I'd expect.
>
>Any solutions?

As suggested in another message, just loop through your 'main' 
(either %main:: or %::) namespace and look for the sub-routine name:

------------------------------------------------------------

my $find = "mysub";
my $find1 = "mysu";

is_sub_defined($find) ? print "$find is defined\n" : print "$find is not defined\n";
is_sub_defined($find1) ? print "$find1 is defined\n" : print "$find1 is not defined\n";

sub mysub(){
    my $x = 10;
}


sub is_sub_defined(){
    my ($sub) = @_;

    for my $keys (keys %::){
        return 1 if $keys eq $sub;
    }

    return 0;
}

------------------------------------------------------------------

One thing to note when working with the symbol table is that any variables
declared via 'my' do not get symbol table entries so if you loop through it
trying to find such a variable, or try to hack such a variable this way you
will find it difficult. Just an FYI.


Shay





More information about the Phoenix-pm mailing list