5 Symbol - manipulate Perl symbols and their names
12 open($sym, "filename");
16 ungensym $sym; # no effect
18 # localize *FOO IO handle but not $FOO, %FOO, etc.
19 my $save_fooio = *FOO{IO} || geniosym;
22 *FOO{IO} = $save_fooio;
24 print qualify("x"), "\n"; # "Test::x"
25 print qualify("x", "FOO"), "\n" # "FOO::x"
26 print qualify("BAR::x"), "\n"; # "BAR::x"
27 print qualify("BAR::x", "FOO"), "\n"; # "BAR::x"
28 print qualify("STDOUT", "FOO"), "\n"; # "main::STDOUT" (global)
29 print qualify(\*x), "\n"; # returns \*x
30 print qualify(\*x, "FOO"), "\n"; # returns \*x
33 print { qualify_to_ref $fh } "foo!\n";
34 $ref = qualify_to_ref $name, $pkg;
36 use Symbol qw(delete_package);
37 delete_package('Foo::Bar');
38 print "deleted\n" unless exists $Foo::{'Bar::'};
43 C<Symbol::gensym> creates an anonymous glob and returns a reference
44 to it. Such a glob reference can be used as a file or directory
47 For backward compatibility with older implementations that didn't
48 support anonymous globs, C<Symbol::ungensym> is also provided.
49 But it doesn't do anything.
51 C<Symbol::geniosym> creates an anonymous IO handle. This can be
52 assigned into an existing glob without affecting the non-IO portions
55 C<Symbol::qualify> turns unqualified symbol names into qualified
56 variable names (e.g. "myvar" -E<gt> "MyPackage::myvar"). If it is given a
57 second parameter, C<qualify> uses it as the default package;
58 otherwise, it uses the package of its caller. Regardless, global
59 variable names (e.g. "STDOUT", "ENV", "SIG") are always qualified with
62 Qualification applies only to symbol names (strings). References are
63 left unchanged under the assumption that they are glob references,
64 which are qualified by their nature.
66 C<Symbol::qualify_to_ref> is just like C<Symbol::qualify> except that it
67 returns a glob ref rather than a symbol name, so you can use the result
68 even if C<use strict 'refs'> is in effect.
70 C<Symbol::delete_package> wipes out a whole package namespace. Note
71 this routine is not exported by default--you may want to import it
76 BEGIN { require 5.005; }
80 @EXPORT = qw(gensym ungensym qualify qualify_to_ref);
81 @EXPORT_OK = qw(delete_package geniosym);
85 my $genpkg = "Symbol::";
88 my %global = map {$_ => 1} qw(ARGV ARGVOUT ENV INC SIG STDERR STDIN STDOUT);
91 # Note that we never _copy_ the glob; we just make a ref to it.
92 # If we did copy it, then SVf_FAKE would be set on the copy, and
93 # glob-specific behaviors (e.g. C<*$ref = \&func>) wouldn't work.
96 my $name = "GEN" . $genseq++;
97 my $ref = \*{$genpkg . $name};
98 delete $$genpkg{$name};
104 # force the IO slot to be filled
113 if (!ref($name) && index($name, '::') == -1 && index($name, "'") == -1) {
115 # Global names: special character, "^xyz", or other.
116 if ($name =~ /^(([^a-z])|(\^[a-z_]+))\z/i || $global{$name}) {
117 # RGS 2001-11-05 : translate leading ^X to control-char
118 $name =~ s/^\^([a-z_])/'qq(\c'.$1.')'/eei;
122 $pkg = (@_ > 1) ? $_[1] : caller;
124 $name = $pkg . "::" . $name;
129 sub qualify_to_ref ($;$) {
130 return \*{ qualify $_[0], @_ > 1 ? $_[1] : caller };
136 sub delete_package ($) {
139 # expand to full symbol table name if needed
141 unless ($pkg =~ /^main::.*::$/) {
142 $pkg = "main$pkg" if $pkg =~ /^::/;
143 $pkg = "main::$pkg" unless $pkg =~ /^main::/;
144 $pkg .= '::' unless $pkg =~ /::$/;
147 my($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
148 my $stem_symtab = *{$stem}{HASH};
149 return unless defined $stem_symtab and exists $stem_symtab->{$leaf};
152 # free all the symbols in the package
154 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH};
155 foreach my $name (keys %$leaf_symtab) {
156 undef *{$pkg . $name};
159 # delete the symbol table
162 delete $stem_symtab->{$leaf};