Typeglobs establish an abstraction layer that relates
symbol table entries to data types ($a @a %a &a a
).
They can be used to avoid de-referencing:
use vars qw( $a *b); $a = 10; *b = *a; $b++; print "a: $a\n"; # -> 11
In the next example an array reference is passed to a subroutine:
use vars qw( @a *b); @a = qw( a b c); print " a: @a \n"; # -> 'a b c' f1( *a); print " a: @a \n"; # -> 'd e f' sub f1 { local( *b) = shift; @b = qw( d e f); }
Notice that typeglobs can be localized. But they can not be
lexical variables (my
).
References ${\$x}
and typeglobs x{*x}
are
equivalent. Thus we have:
*b = \$a; # selective aliasing *PI = \3.1415927; # read-only variables *f1 = sub {print "hello\n";} # named anonymous subroutines f1();