Programming Perl

Programming PerlSearch this book
Previous: 7.2.60 subs - Predeclare Subroutine NamesChapter 7
The Standard Perl Library
Next: 7.2.62 Sys::Hostname - Try Every Conceivable Way to Get Hostname
 

7.2.61 Symbol - Generate Anonymous Globs; Qualify Variable Names

use Symbol;

$sym = gensym;
open($sym, "filename");
$_ = <$sym>;

ungensym $sym;      # no effect

print qualify("x");              # "main::x"
print qualify("x", "FOO");       # "FOO::x"
print qualify("BAR::x");         # "BAR::x"
print qualify("BAR::x", "FOO");  # "BAR::x"
print qualify("STDOUT", "FOO");  # "main::STDOUT" (global)
print qualify(\*x);              # \*x--for example: GLOB(0x99530)
print qualify(\*x, "FOO");       # \*x--for example: GLOB(0x99530)

gensym() creates an anonymous glob and returns a reference to it. Such a glob reference can be used as a filehandle or directory handle.

For backward compatibility with older implementations that didn't support anonymous globs, ungensym() is also provided. But it doesn't do anything.

qualify() turns unqualified symbol names into qualified variable names (for example, myvar becomes MyPackage::myvar). If it is given a second parameter, qualify() uses it as the default package; otherwise, it uses the package of its caller. Regardless, global variable names (for example, STDOUT, %ENV, %SIG) are always qualified with main::.

Qualification applies only to symbol names (strings). References are left unchanged under the assumption that they are glob references, which are qualified by their nature.


Previous: 7.2.60 subs - Predeclare Subroutine NamesProgramming PerlNext: 7.2.62 Sys::Hostname - Try Every Conceivable Way to Get Hostname
7.2.60 subs - Predeclare Subroutine NamesBook Index7.2.62 Sys::Hostname - Try Every Conceivable Way to Get Hostname