Herong's Tutorial Notes on Perl - Part A
Dr. Herong Yang, Version 4.09

Typeglob and Package Exporter

Part:   1   2 

This chapter describes:

  • What are typeglobs and how to use them to define identifier aliases.
  • How to access variables from other packages as aliases.
  • How to use Exporter to import variables from other packages.

Typeglob, Symbolic Table and Identifier Aliases

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 hash name is actually the name space 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{name}.
  • Using * notation. For example, *main::name.

Since the same identifier can be used for different data types, there will be only one entry in the symbol table 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 same places 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
#
   &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] =

(Continued on next part...)

Part:   1   2 

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Perl - Part A - Typeglob and Package Exporter