Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.00

Typeglob, Symbolic Table and Identifier Aliases

This section describes symbol table and, typeglob and identifier aliases. The symbol table is a hash used by the compiler to store all identifiers used in the package.

Typeglob: A special data type used by Perl compiler for the symbol table.

Symbol Table: A hash used by Perl compiler for each name space (package). The name of the symbol table hash is actually the package name followed with a double-colon, ::.

Keys in the symbol table are identifiers used in the name space. Values associated with keys are typeglobs.

There are two ways to access typeglobs:

  • Using hash notation. For example, $main{identifier}.
  • Using * notation. For example, *main::identifier.

Since the same identifier can be used for different data types, there will be only one entry in the symbol table for many variables of different data types, if they have the sample identifier.

One common usage of typeglobs is to create identifier aliases. There are several ways to create aliases:

1. Assigning one typeglob to another with "*" notations to create an alias for all data types. For example, "*main::foo = *main::bar" - "foo" is an alias for "bar" for all data types.

2. Assigning one typeglob to another with hash notations to create an alias for all data types. For example, "$main::{foo} = $main::{bar}" - "foo" is an alias for "bar" for all data types.

3. Assigning a hard reference to a typeglob to create an alias for one data type. For example "*main::foo = \$main::bar" - "foo" is an alias for "bar" for scalar varialbe.

Aliases can be used in the same place as the original identifier.

Here is a program to show you how to create and use identifier aliases:

#- TypeglobTest.pl
#- Copyright (c) 1999 by Dr. Herong Yang, http://www.herongyang.com/
#
   &printHash("main::");
   $x = 3.14;
   %x = (k1,9,k2,99,k3,999,k4,999);
   *y = *x;
   $a = "hello";
   @a = (apple,banana,orange);
   *b = \$a;
   print "\$y = $y\n";
   print "\$y{k3} = $y{k3}\n";
   print "\$b = $b\n";
   print "\$b[1] = $b[1]\n";
   exit;
sub printHash {
   local ($hashName) = @_; 
   foreach $key (sort keys %$hashName) {
      print "$key = $$hashName{$key}\n";
   }
}

Here is the output with ActivePerl:

 = *main::
? = *main::?
? = *main::?
" = *main::"
$ = *main::$
/ = *main::/
0 = *main::0
@ = *main::@
ARGV = *main::ARGV
CORE:: = *main::CORE::
DB:: = *main::DB::
DynaLoader:: = *main::DynaLoader::
ENV = *main::ENV
INC = *main::INC
IO:: = *main::IO::
STDERR = *main::STDERR
STDIN = *main::STDIN
STDOUT = *main::STDOUT
UNIVERSAL:: = *main::UNIVERSAL::
Win32:: = *main::Win32::
a = *main::a
attributes:: = *main::attributes::
b = *main::b
hashName = *main::hashName
key = *main::key
main:: = *main::main::
printHash = *main::printHash
stderr = *main::stderr
stdin = *main::stdin
stdout = *main::stdout
x = *main::x
y = *main::y
$y = 3.14
$y{k3} = 999
$b = hello
$b[1] =

Couple of interesting notes:

  • The first entry in the symbol table defines an empty string key. What's for?
  • Identifier alias assignment statements seem to be processes during the compilation. This is why the symbolic table printed at the beginning of the execution already contains "y" and "b".
  • $b[1] is undefined, because "b" is defined as an alias only for scalar variable.
  • I am using a symbolic reference to pass the hash variable name into a subroutine.

Sections in This Chapter

Typeglob, Symbolic Table and Identifier Aliases

Accessing Identifiers from Other Packages as Aliases

Exporting and Importing Package Identifiers

Dr. Herong Yang, updated in 2008
Typeglob, Symbolic Table and Identifier Aliases