Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Perplexed by typeglobs.

by Anonymous Monk
on Feb 26, 2005 at 06:52 UTC ( #434739=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks.

I have seen numerous perl programs written that include the following:
A co-worker told me they were typeglobs but nothing further was mentioned. Could one of you tell me how and why they are used (maybe a short program with an explanation)? Right now, I feel my typeglob related ignorance is hampering my study of perl.

Thank you monks,


Replies are listed 'Best First'.
Re: Perplexed by typeglobs.
by Ovid (Cardinal) on Feb 26, 2005 at 07:16 UTC

    A typeglob is a slot in a symbol table. Essentially, every symbol table is a hash with keys being the names of globs and the values being the typeglobs (the terms "glob" and "typeglob" are frequently used interchangeably). Each typeglob has a variety of "slots" which correspond to each data type that Perl allows (scalars, arrays, hashes, filehandles, etc.) In the case of the typeglobs you listed, they are probably filehandles, though there is nothing to prevent someone from using %STDIN as a variable name which, if a package variable, could be accessed via the *STDIN typeglob.

    I realize this probably did not answer your question, but that's because there's a lot to talk about for this particular topic. I would suggest consulting perldata and perlmod for an introduction to typeglobs and symbol tables.


    New address of my CGI Course.

      Also, one of the better treatments of typeglobs and symbol tables that I've found actually lives in Damian Conway's book "Object Oriented Perl" (ISBN:ISBN 1884777791).


      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Perplexed by typeglobs.
by sauoq (Abbot) on Feb 26, 2005 at 10:49 UTC
    Right now, I feel my typeglob related ignorance is hampering my study of perl.

    Don't worry... it probably isn't. ;-)

    It is rarely necessary to use typeglobs directly. Especially these days (so long as you use a current version of perl) since support for using lexicals for filehandles has been built into open(). Passing filehandles was once their most common use.

    Another use for them that doesn't get as much attention nowadays as I think it should is to create constants. Try the following code for example...

    *foo = \"foo"; print "foo is $foo.\n"; $foo = "bar"; # Run time error. Modification of read-only value att +empted.
    I prefer this to use constant FOO => "foo"; since those constants don't interpolate in strings (without jumping through the right hoops.)

    I'm not suggesting that you shouldn't learn about them, of course. Only that you probably don't need them. They are indispensable for some introspective tasks and if you are writing a debugger or data dumper or something, you might need them afterall. But, most tasks can (and should) be done without them.

    "My two cents aren't worth a dime.";
Re: Perplexed by typeglobs.
by chas (Priest) on Feb 26, 2005 at 07:33 UTC
    For each identifier in a package, there is a symbol table entry containing slots for each of the possible types with that identifier as its "name". E.g. if we have a variable $x, a subroutine &x, an array @x, these are all held in a typeglob *x. If one sets *y = *x, then *y contains the same slots that *x does, so, for example, $y is the same as $x. One can access slots of a typglob: for example, *x{SCALAR} is a reference to $x. Using typeglobs or references to typeglobs one can pass filehandles to a subroutine. For example,
    sub blah { my $filehandle = shift; print $filehandle "Blah!\n"; } blah(*STDOUT); #these both blah(\*STDOUT); #produce Blah! (and a newline)
    (Updated to fix typo.)
Re: Perplexed by typeglobs.
by ikegami (Pope) on Feb 26, 2005 at 07:12 UTC
    *var is a reference to the symbol table entry for $var, @var, %var, &var, etc. Most functions (all builtins?) that accept a bareword as a file handle also accept a reference to the symbol as an alternative. The difference is that references can be assigned, and therefore don't need to be hardcoded as function arguments like barewords are. ($fh = *FILE1; print $fh "...";)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://434739]
Approved by kvale
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2020-08-08 21:03 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (53 votes). Check out past polls.