5 Symbol - manipulate Perl symbols and their names
12 open($sym, "filename");
16 ungensym $sym; # no effect
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
28 C<Symbol::gensym> creates an anonymous glob and returns a reference
29 to it. Such a glob reference can be used as a file or directory
32 For backward compatibility with older implementations that didn't
33 support anonymous globs, C<Symbol::ungensym> is also provided.
34 But it doesn't do anything.
36 C<Symbol::qualify> turns unqualified symbol names into qualified
37 variable names (e.g. "myvar" -> "MyPackage::myvar"). If it is given a
38 second parameter, C<qualify> uses it as the default package;
39 otherwise, it uses the package of its caller. Regardless, global
40 variable names (e.g. "STDOUT", "ENV", "SIG") are always qualfied with
43 Qualification applies only to symbol names (strings). References are
44 left unchanged under the assumption that they are glob references,
45 which are qualified by their nature.
54 @EXPORT = qw(gensym ungensym qualify);
56 my $genpkg = "Symbol::";
67 my $name = "GEN" . $genseq++;
68 local *{$genpkg . $name};
69 \delete ${$genpkg}{$name};
76 if (! ref($name) && $name !~ /::/) {
78 # Global names: special character, "^x", or other.
79 if ($name =~ /^([^a-z])|(\^[a-z])$/i || $global{$name}) {
83 $pkg = (@_ > 1) ? $_[1] : caller;
85 $name = $pkg . "::" . $name;