Phoenix.pm: Symbol Tables part deu

Shay Harding mekla at geocities.com
Mon Oct 4 22:30:33 CDT 1999


>Thanks for the two code examples.
>
>But the only problem that I can see is if there were also a variable of the 
>same name, then this would also test positive.  So, more specifically, can I 
>test to see if the {CODE} portion of the symbol table is filled for a 
>specific name?

Hey, this is Perl. Of course you can! :)

>
>This may be a stupid and minor 'problem' with other workarounds, but I feel 
>that if I work around it, then I may be missing a major concept as to the 
>inner workings of Perl.

Reference the Devel::* modules to learn more.

Here is a piece of code that may guide you in the right direction:


--------------------- START OF CODE --------------------------------

#!/usr/bin/perl5

system "clear";


package A;

$risk = "Help";
@risk = qw(HelpA);
%risk = ('1' => 'a', '2' => 'b', '3' => 'c');

sub risk(){
    return hex($_[0]);
}

open (RISK,">test.txt");


package B;

while (($key,$val) = each(%{*{"A\::"}})) {
    local(*ENTRY) = $val;

    #### SCALAR ####
    if (defined $val && defined *ENTRY{SCALAR}) {
        print "$key => $val (SCALAR)\n";
    }

    #### ARRAY ####
    if (defined $val && defined *ENTRY{ARRAY}) {
        print "$key => $val (ARRAY)\n";
    }

    #### HASH ####
    if (defined $val && defined *ENTRY{HASH} && $key !~ /::/) {
        print "$key => $val (HASH)\n";
    }

    #### FUNCTION ####
    if (defined $val && defined *ENTRY{CODE}) {
        print "$key => $val (CODE)\n";
    }

    #### IO ####
    if ($] > 5.003_10){
        if (defined $val && defined *ENTRY{IO}){
            print "$key => $val (IO)\n";
        }
    }
    else{
        #### FILEHANDLE ####
        if (defined fileno(ENTRY)){
            print "$key => $val (FILEHANDLE)\n";
        }
        #### DIRHANDLE ####
        elsif (defined telldir(ENTRY)){
            print "$key => $val (DIRHANDLE)\n";
        }
    }
}


------------------------- END OF CODE ---------------------------



>Thanks,
>
>Tim
>
>(P.S. As a further brain teaser:  I know you can delete a symbol from the 
>symbol table, as you can with any other hash.  But can you delete just the 
>{CODE} portion of the symbol table of a particular name?  But of course, if 
>the first dillemma is figured out, then this will probably be answered at the 
>same time...)


Not sure if you can pick and choose to delete one part of the symbol table
especially if you have a scalar and sub with the same name. This is because
the entry in the symbol table is basically a pointer to the actual values
(a typeglob):

*some_name ->  $some_name
	       ->  &some_name


They all basically carry the same name as far as Perl is concerned, however
since there are multiple pointers from one typeglob to the many actual
values, it stands to reason that the typeglob be some sort of hash table
(mini-symbol table) in of itself with internal IDs as keys. So maybe they
can be dereferenced somehow, although I am not sure how? If you try to get
the value you get something like:

*PACKAGE::some_name

but it won't dereference the value of a particular type.

Maybe some construct like:

${*{"PACKAGE\::some_name"}}
@{*{"PACKAGE\::some_name"}}
etc

Don't know, haven't tried it yet. If you figure it out let me know.


-------

Shay





More information about the Phoenix-pm mailing list