4

While reading about the pragma overload of Perl5, I noticed the operator *{}.

I'd like to know what kind of sigil * is, if any, or what's the meaning of * in a ref context.

tchrist
  • 78,834
  • 30
  • 123
  • 180
mgodinho
  • 155
  • 2
  • 6

2 Answers2

4

It refers to a typeglob. It can refer to any type. They are mostly legacy now.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    Not legacy. `IO::Socket` and its subclasses use typeglobs to store configuration about each sockethandle. – mob Apr 13 '12 at 15:27
  • "Typeglob" (or "glob" for short) is Perl's name for a "symbol table entry". The symbol table holds all symbols that belong to a package and all "truly global" variables (e.g. `$1`). Without the symbol, there would be no named subroutines, `@ISA` and `@EXPORT` wouldn't exist, and neither would `$_`, `@_`, `$$`, etc. Globs are most definitely *not* legacy. Globs are also used to wrap IO objects (file handles) including lexical file handles, although that could be for legacy reasons since IO objects are now first-class objects. – ikegami Apr 13 '12 at 18:22
  • @mob, While globs aren't legacy, your comment is not a valid argument against globs being legacy. They only reason IO::Socket uses globs is because IO objects are wrapped in them, and since that may be for legacy reasons, IO::Socket could be doing so for legacy reasons. – ikegami Apr 13 '12 at 18:23
  • I was using `IO::Socket` and its ilk as examples [with specific needs for dereferencing typeglobs](http://stackoverflow.com/questions/9795926). – mob Apr 13 '12 at 19:57
3

*foo is a "typeglob", or "glob" for short. A glob is a structure (as in C struct) with fields named ARRAY, HASH, IO, etc. These fields either contain nothing or a reference. From the perspective of Perl code, they look like very special hashes.

The primary purpose of globs is to server as entries for Perl's symbol table. The symbol table holds all symbols that belong to a package and all "truly global" variables (e.g. STDOUT, $1, etc). Without the symbol, there would be no named subroutines, @ISA and @EXPORT wouldn't exist, and neither would @_, $$, etc. [Obviously, globs are most definitely not legacy.]

$ perl -E'
    our @foo = qw( a b c );
    our %foo = ( d=>4, e=>5 );
    say @{ *foo{ARRAY} };
    say %{ *foo{HASH} };
'
abc
d4e5

Globs are also used as wrappers around IO objects (file handles). Even open(my $fh, ...) populates $fh with a glob.

Globs are very rarely used explicitly in Perl. The one exception is old-style file handles. For example, FILE and STDOUT actually means *FILE and *STDOUT (when used as file handles), which in term are used to get *FILE{IO} and *STDOUT{IO}.

$ perl -e'open(FILE, "echo foo|") or die; print readline(FILE);'
foo

$ perl -e'open(*FILE, "echo foo|") or die; print readline(*FILE);'
foo

$ perl -e'open(*FILE{IO}, "echo foo|") or die; print readline(*FILE{IO});'
foo

So why would you want to override *{}?

You would want to override *{} if you wanted to create an object that looks like a file handle without actually being a file handle. For example, you could use this override to make IO::Socket object hash-based objects instead of glob-based objects.

ikegami
  • 367,544
  • 15
  • 269
  • 518