I have never found the documentation for typeglobs particularly clear myself, even when typeglobs were more important to routine Perl programming than they are now (references are a better way of doing many of things that typeglobs were used for before).
My favorite introduction to typeglobs is ch. 3 of the venerable Advanced Perl Programming (1st edition, 1997), by Sriram Srinivasan. (The much awaited 2nd edition of this book, by Simon Cozens, is a complete re-write, and will not cover this topic.)
Other sources worth consulting are the Symbol Tables section of perlmod and the Typeglobs and Filehandles section of perldata. These sources have a better treatment (than APP's) of the remaining uses for typeglobs in Perl 5.
| [reply] [d/l] [select] |
I use them as shortcut/alias from time to time:
*xyz = \&X::YY::ZZ::function;
or as a alias for a scalar's:
my $string = 'Hi There';
local *_ = \$string;
s/\s+/ /g;
print $string;
__OUTPUT__
Hi There
| [reply] [d/l] [select] |
I made some comments on typeglobs in 434748 (the thread is at 434739.)
The O'Reilly book "Programming Perl" (The Camel Book) has info about them, and try "perldoc perldata" also.
chas | [reply] |
| [reply] |
Short answer: a typeglob is a symbol table entry; if you have (non-lexical) $foo, %foo, @foo, &foo, and format foo, they are all contained in the same typeglob *foo (along with a file/dir handle). Because filehandles have no specific sigil, you can only mention them in perl by way of the glob.
So there are two cases where you'd use a typeglob: as a filehandle, or to muck about with the symbol table.
The most common case of the former is to use one of the
perl provided filehandles *STDIN, *STDOUT, *STDERR, *DATA. (The * is unneeded if passing to a function or builtin having a * in the prototype.) For non-perl provided filehandles, using the newer lexical filehandles (actually just a reference to a typeglob, stored in a regular scalar variable) obviates the need for a typeglob. | [reply] |