Re: [ID 20020213.015] Pod::Html XHTML update for 5.7.2
[p5sagit/p5-mst-13.2.git] / lib / Symbol.pm
CommitLineData
c07a80fd 1package Symbol;
2
3=head1 NAME
4
5Symbol - 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 print qualify("x"), "\n"; # "Test::x"
19 print qualify("x", "FOO"), "\n" # "FOO::x"
20 print qualify("BAR::x"), "\n"; # "BAR::x"
21 print qualify("BAR::x", "FOO"), "\n"; # "BAR::x"
22 print qualify("STDOUT", "FOO"), "\n"; # "main::STDOUT" (global)
23 print qualify(\*x), "\n"; # returns \*x
24 print qualify(\*x, "FOO"), "\n"; # returns \*x
25
b42fedfb 26 use strict refs;
27 print { qualify_to_ref $fh } "foo!\n";
28 $ref = qualify_to_ref $name, $pkg;
29
1ee082b7 30 use Symbol qw(delete_package);
31 delete_package('Foo::Bar');
32 print "deleted\n" unless exists $Foo::{'Bar::'};
33
34
c07a80fd 35=head1 DESCRIPTION
36
37C<Symbol::gensym> creates an anonymous glob and returns a reference
38to it. Such a glob reference can be used as a file or directory
39handle.
40
41For backward compatibility with older implementations that didn't
42support anonymous globs, C<Symbol::ungensym> is also provided.
43But it doesn't do anything.
44
45C<Symbol::qualify> turns unqualified symbol names into qualified
7c584b33 46variable names (e.g. "myvar" -E<gt> "MyPackage::myvar"). If it is given a
c07a80fd 47second parameter, C<qualify> uses it as the default package;
48otherwise, it uses the package of its caller. Regardless, global
f610777f 49variable names (e.g. "STDOUT", "ENV", "SIG") are always qualified with
c07a80fd 50"main::".
51
52Qualification applies only to symbol names (strings). References are
53left unchanged under the assumption that they are glob references,
54which are qualified by their nature.
55
b42fedfb 56C<Symbol::qualify_to_ref> is just like C<Symbol::qualify> except that it
57returns a glob ref rather than a symbol name, so you can use the result
58even if C<use strict 'refs'> is in effect.
59
1ee082b7 60C<Symbol::delete_package> wipes out a whole package namespace. Note
61this routine is not exported by default--you may want to import it
62explicitly.
63
c07a80fd 64=cut
65
c74f62b5 66BEGIN { require 5.005; }
c07a80fd 67
68require Exporter;
69@ISA = qw(Exporter);
b42fedfb 70@EXPORT = qw(gensym ungensym qualify qualify_to_ref);
1ee082b7 71@EXPORT_OK = qw(delete_package);
c07a80fd 72
c74f62b5 73$VERSION = 1.04;
c07a80fd 74
75my $genpkg = "Symbol::";
76my $genseq = 0;
77
7c584b33 78my %global = map {$_ => 1} qw(ARGV ARGVOUT ENV INC SIG STDERR STDIN STDOUT);
c07a80fd 79
6adf1df6 80#
81# Note that we never _copy_ the glob; we just make a ref to it.
82# If we did copy it, then SVf_FAKE would be set on the copy, and
83# glob-specific behaviors (e.g. C<*$ref = \&func>) wouldn't work.
84#
c07a80fd 85sub gensym () {
86 my $name = "GEN" . $genseq++;
6adf1df6 87 my $ref = \*{$genpkg . $name};
88 delete $$genpkg{$name};
89 $ref;
c07a80fd 90}
91
92sub ungensym ($) {}
93
94sub qualify ($;$) {
95 my ($name) = @_;
49da0595 96 if (!ref($name) && index($name, '::') == -1 && index($name, "'") == -1) {
c07a80fd 97 my $pkg;
c74f62b5 98 # Global names: special character, "^xyz", or other.
99 if ($name =~ /^(([^a-z])|(\^[a-z_]+))\z/i || $global{$name}) {
100 # RGS 2001-11-05 : translate leading ^X to control-char
101 $name =~ s/^\^([a-z_])/'qq(\c'.$1.')'/eei;
c07a80fd 102 $pkg = "main";
103 }
104 else {
105 $pkg = (@_ > 1) ? $_[1] : caller;
106 }
107 $name = $pkg . "::" . $name;
108 }
109 $name;
110}
111
b42fedfb 112sub qualify_to_ref ($;$) {
113 return \*{ qualify $_[0], @_ > 1 ? $_[1] : caller };
114}
115
1ee082b7 116#
117# of Safe.pm lineage
118#
119sub delete_package ($) {
120 my $pkg = shift;
121
122 # expand to full symbol table name if needed
123
124 unless ($pkg =~ /^main::.*::$/) {
125 $pkg = "main$pkg" if $pkg =~ /^::/;
126 $pkg = "main::$pkg" unless $pkg =~ /^main::/;
127 $pkg .= '::' unless $pkg =~ /::$/;
128 }
129
130 my($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/;
131 my $stem_symtab = *{$stem}{HASH};
132 return unless defined $stem_symtab and exists $stem_symtab->{$leaf};
133
c2e66d9e 134
135 # free all the symbols in the package
136
137 my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH};
138 foreach my $name (keys %$leaf_symtab) {
139 undef *{$pkg . $name};
140 }
141
142 # delete the symbol table
1ee082b7 143
144 %$leaf_symtab = ();
145 delete $stem_symtab->{$leaf};
146}
147
c07a80fd 1481;