This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Locale::Codes 2.02.
[perl5.git] / lib / Symbol.pm
1 package Symbol;
2
3 =head1 NAME
4
5 Symbol - manipulate Perl symbols and their names
6
7 =head1 SYNOPSIS
8
9     use Symbol;
10
11     $sym = gensym;
12     open($sym, "filename");
13     $_ = <$sym>;
14     # etc.
15
16     ungensym $sym;      # no effect
17
18     # replace *FOO{IO} handle but not $FOO, %FOO, etc.
19     *FOO = geniosym;
20
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
28
29     use strict refs;
30     print { qualify_to_ref $fh } "foo!\n";
31     $ref = qualify_to_ref $name, $pkg;
32
33     use Symbol qw(delete_package);
34     delete_package('Foo::Bar');
35     print "deleted\n" unless exists $Foo::{'Bar::'};
36
37
38 =head1 DESCRIPTION
39
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
42 handle.
43
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.
47
48 C<Symbol::geniosym> creates an anonymous IO handle.  This can be
49 assigned into an existing glob without affecting the non-IO portions
50 of the glob.
51
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
57 "main::".
58
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.
62
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.
66
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
69 explicitly.
70
71 =cut
72
73 BEGIN { require 5.005; }
74
75 require Exporter;
76 @ISA = qw(Exporter);
77 @EXPORT = qw(gensym ungensym qualify qualify_to_ref);
78 @EXPORT_OK = qw(delete_package geniosym);
79
80 $VERSION = 1.04;
81
82 my $genpkg = "Symbol::";
83 my $genseq = 0;
84
85 my %global = map {$_ => 1} qw(ARGV ARGVOUT ENV INC SIG STDERR STDIN STDOUT);
86
87 #
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.
91 #
92 sub gensym () {
93     my $name = "GEN" . $genseq++;
94     my $ref = \*{$genpkg . $name};
95     delete $$genpkg{$name};
96     $ref;
97 }
98
99 sub geniosym () {
100     my $sym = gensym();
101     # force the IO slot to be filled
102     select(select $sym);
103     *$sym{IO};
104 }
105
106 sub ungensym ($) {}
107
108 sub qualify ($;$) {
109     my ($name) = @_;
110     if (!ref($name) && index($name, '::') == -1 && index($name, "'") == -1) {
111         my $pkg;
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;
116             $pkg = "main";
117         }
118         else {
119             $pkg = (@_ > 1) ? $_[1] : caller;
120         }
121         $name = $pkg . "::" . $name;
122     }
123     $name;
124 }
125
126 sub qualify_to_ref ($;$) {
127     return \*{ qualify $_[0], @_ > 1 ? $_[1] : caller };
128 }
129
130 #
131 # of Safe.pm lineage
132 #
133 sub delete_package ($) {
134     my $pkg = shift;
135
136     # expand to full symbol table name if needed
137
138     unless ($pkg =~ /^main::.*::$/) {
139         $pkg = "main$pkg"       if      $pkg =~ /^::/;
140         $pkg = "main::$pkg"     unless  $pkg =~ /^main::/;
141         $pkg .= '::'            unless  $pkg =~ /::$/;
142     }
143
144     my($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
145     my $stem_symtab = *{$stem}{HASH};
146     return unless defined $stem_symtab and exists $stem_symtab->{$leaf};
147
148
149     # free all the symbols in the package
150
151     my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH};
152     foreach my $name (keys %$leaf_symtab) {
153         undef *{$pkg . $name};
154     }
155
156     # delete the symbol table
157
158     %$leaf_symtab = ();
159     delete $stem_symtab->{$leaf};
160 }
161
162 1;