5 Symbol - manipulate Perl symbols and their names
12 open($sym, "filename");
16 ungensym $sym; # no effect
18 # replace *FOO{IO} handle but not $FOO, %FOO, etc.
21 print qualify("x"), "\n"; # "Test::x"
22 print qualify("x", "FOO"), "\n" # "FOO::x"
23 print qualify("BAR::x"), "\n"; # "BAR::x"
24 print qualify("BAR::x", "FOO"), "\n"; # "BAR::x"
25 print qualify("STDOUT", "FOO"), "\n"; # "main::STDOUT" (global)
26 print qualify(\*x), "\n"; # returns \*x
27 print qualify(\*x, "FOO"), "\n"; # returns \*x
30 print { qualify_to_ref $fh } "foo!\n";
31 $ref = qualify_to_ref $name, $pkg;
33 use Symbol qw(delete_package);
34 delete_package('Foo::Bar');
35 print "deleted\n" unless exists $Foo::{'Bar::'};
40 C<Symbol::gensym> creates an anonymous glob and returns a reference
41 to it. Such a glob reference can be used as a file or directory
44 For backward compatibility with older implementations that didn't
45 support anonymous globs, C<Symbol::ungensym> is also provided.
46 But it doesn't do anything.
48 C<Symbol::geniosym> creates an anonymous IO handle. This can be
49 assigned into an existing glob without affecting the non-IO portions
52 C<Symbol::qualify> turns unqualified symbol names into qualified
53 variable names (e.g. "myvar" -E<gt> "MyPackage::myvar"). If it is given a
54 second parameter, C<qualify> uses it as the default package;
55 otherwise, it uses the package of its caller. Regardless, global
56 variable names (e.g. "STDOUT", "ENV", "SIG") are always qualified with
59 Qualification applies only to symbol names (strings). References are
60 left unchanged under the assumption that they are glob references,
61 which are qualified by their nature.
63 C<Symbol::qualify_to_ref> is just like C<Symbol::qualify> except that it
64 returns a glob ref rather than a symbol name, so you can use the result
65 even if C<use strict 'refs'> is in effect.
67 C<Symbol::delete_package> wipes out a whole package namespace. Note
68 this routine is not exported by default--you may want to import it
73 BEGIN { require 5.005; }
77 @EXPORT = qw(gensym ungensym qualify qualify_to_ref);
78 @EXPORT_OK = qw(delete_package geniosym);
82 my $genpkg = "Symbol::";
85 my %global = map {$_ => 1} qw(ARGV ARGVOUT ENV INC SIG STDERR STDIN STDOUT);
88 # Note that we never _copy_ the glob; we just make a ref to it.
89 # If we did copy it, then SVf_FAKE would be set on the copy, and
90 # glob-specific behaviors (e.g. C<*$ref = \&func>) wouldn't work.
93 my $name = "GEN" . $genseq++;
94 my $ref = \*{$genpkg . $name};
95 delete $$genpkg{$name};
101 # force the IO slot to be filled
110 if (!ref($name) && index($name, '::') == -1 && index($name, "'") == -1) {
112 # Global names: special character, "^xyz", or other.
113 if ($name =~ /^(([^a-z])|(\^[a-z_]+))\z/i || $global{$name}) {
114 # RGS 2001-11-05 : translate leading ^X to control-char
115 $name =~ s/^\^([a-z_])/'qq(\c'.$1.')'/eei;
119 $pkg = (@_ > 1) ? $_[1] : caller;
121 $name = $pkg . "::" . $name;
126 sub qualify_to_ref ($;$) {
127 return \*{ qualify $_[0], @_ > 1 ? $_[1] : caller };
133 sub delete_package ($) {
136 # expand to full symbol table name if needed
138 unless ($pkg =~ /^main::.*::$/) {
139 $pkg = "main$pkg" if $pkg =~ /^::/;
140 $pkg = "main::$pkg" unless $pkg =~ /^main::/;
141 $pkg .= '::' unless $pkg =~ /::$/;
144 my($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
145 my $stem_symtab = *{$stem}{HASH};
146 return unless defined $stem_symtab and exists $stem_symtab->{$leaf};
149 # free all the symbols in the package
151 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH};
152 foreach my $name (keys %$leaf_symtab) {
153 undef *{$pkg . $name};
156 # delete the symbol table
159 delete $stem_symtab->{$leaf};