Typeglobs

Typeglobs, we mentioned earlier, can be localized (with local only) and assigned to one another. Assigning a typeglob has the effect of aliasing one identifier name to another. Consider

$spud   = "Wow!";
@spud   = ("idaho", "russet");
*potato= *spud;   # Alias potato to spud using typeglob assignment
print "$potato\n"; # prints "Wow!"
print @potato, "\n"; # prints "idaho russet"

Once the typeglob assignment is made, all entities that were called “spud” can now also be referred to as “potato”—the names are freely interchangeable. That is, $spud and $potato are the same thing, and so are the subroutines &spud and &potato. Figure 3.2 shows the picture after a typeglob assignment; both entries in the symbol table end up pointing to the same typeglob value.[19]

Assigning *spud to *potato: both symbol table entries point to the same typeglob

Figure 3-2. Assigning *spud to *potato: both symbol table entries point to the same typeglob

The alias holds true until the typeglob is reassigned or removed. (We will shortly see how to remove a typeglob.) In the example, there is no subroutine called spud, but if we define it after the typeglobs have been assigned, that subroutine can also be invoked as potato. It turns out that the alias works the other way too. If you assign a new list to @potato, it will also be automatically accessible as @spud.

Temporary Aliases

For now, there is no easy, intuitive way to get rid of an alias created by a typeglob assignment (you may reassign ...

Get Advanced Perl Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.