next up previous contents
Next: The require Compiler Directive Up: Perl Modules Previous: The END Block

Symbol Tables

Each namespace -- and therefore, each module, class, or package -- has its own symbol table. A symbol table, in Perl, is a hash that holds all of the names defined in a namespace. All of the variable and function names can be found there. The hash for each namespace is named after the namespace with two colons. For example, the symbol table for the Foo namespace is called %Foo::.

The next example shows a program that displays all of the entries in the Foo:: namespace. It basically operates as follows:

The Perl code to do this is as follows, symbol.pl:

sub dispSymbols {

    my($hashRef) = shift;

    my(%symbols);

    my(@symbols);



    %symbols = %{$hashRef};

    @symbols = sort(keys(%symbols));



    foreach (@symbols) {

        printf("%-10.10s| %s\n", $_, $symbols{$_});

    }

}



dispSymbols(\%Foo::);



package Foo;

    $bar = 2;



    sub baz {

        $bar++;

    }

This program displays:

bar       | *Foo::bar
baz       | *Foo::baz

This example shows that there are only two things in the %Foo:: symbol table-only those things that the script placed there. This is not the case with the %main:: symbol table. When I display the entries in %main::, I see over 85 items. Part of the reason for the large number of names in the main package is that some variables are forced there. For example, STDIN, STDOUT, STDERR, @ARGV, @ARGVOUT, %ENV, @Inc, and %SIG are forced into the main namespace regardless of when they are used.


next up previous contents
Next: The require Compiler Directive Up: Perl Modules Previous: The END Block
dave@cs.cf.ac.uk