38927d995af2ad204285e78b72209b5643616dd9
[p5sagit/p5-mst-13.2.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     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
26     use strict refs;
27     print { ref_qualify $fh } "foo!\n";
28     $ref = ref_qualify $name, $pkg;
29
30 =head1 DESCRIPTION
31
32 C<Symbol::gensym> creates an anonymous glob and returns a reference
33 to it.  Such a glob reference can be used as a file or directory
34 handle.
35
36 For backward compatibility with older implementations that didn't
37 support anonymous globs, C<Symbol::ungensym> is also provided.
38 But it doesn't do anything.
39
40 C<Symbol::qualify> turns unqualified symbol names into qualified
41 variable names (e.g. "myvar" -E<gt> "MyPackage::myvar").  If it is given a
42 second parameter, C<qualify> uses it as the default package;
43 otherwise, it uses the package of its caller.  Regardless, global
44 variable names (e.g. "STDOUT", "ENV", "SIG") are always qualfied with
45 "main::".
46
47 Qualification applies only to symbol names (strings).  References are
48 left unchanged under the assumption that they are glob references,
49 which are qualified by their nature.
50
51 C<Symbol::ref_qualify> is just like C<Symbol::qualify> except that it
52 returns a glob ref rather than a symbol name.  You can use the result
53 even if C<use strict 'refs'> is in effect.
54
55 =cut
56
57 BEGIN { require 5.002; }
58
59 require Exporter;
60 @ISA = qw(Exporter);
61
62 @EXPORT = qw(gensym ungensym qualify ref_qualify);
63
64 $VERSION = 1.02;
65
66 my $genpkg = "Symbol::";
67 my $genseq = 0;
68
69 my %global = map {$_ => 1} qw(ARGV ARGVOUT ENV INC SIG STDERR STDIN STDOUT);
70
71 #
72 # Note that we never _copy_ the glob; we just make a ref to it.
73 # If we did copy it, then SVf_FAKE would be set on the copy, and
74 # glob-specific behaviors (e.g. C<*$ref = \&func>) wouldn't work.
75 #
76 sub gensym () {
77     my $name = "GEN" . $genseq++;
78     my $ref = \*{$genpkg . $name};
79     delete $$genpkg{$name};
80     $ref;
81 }
82
83 sub ungensym ($) {}
84
85 sub qualify ($;$) {
86     my ($name) = @_;
87     if (!ref($name) && index($name, '::') == -1 && index($name, "'") == -1) {
88         my $pkg;
89         # Global names: special character, "^x", or other. 
90         if ($name =~ /^([^a-z])|(\^[a-z])$/i || $global{$name}) {
91             $pkg = "main";
92         }
93         else {
94             $pkg = (@_ > 1) ? $_[1] : caller;
95         }
96         $name = $pkg . "::" . $name;
97     }
98     $name;
99 }
100
101 sub ref_qualify ($;$) {
102     return \*{ qualify $_[0], @_ > 1 ? $_[1] : caller };
103 }
104
105 1;