1Symbol(3pm) Perl Programmers Reference Guide Symbol(3pm)
2
3
4
6 Symbol - manipulate Perl symbols and their names
7
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"; # "main::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
38 "Symbol::gensym" creates an anonymous glob and returns a reference to
39 it. Such a glob reference can be used as a file or directory handle.
40
41 For backward compatibility with older implementations that didn't
42 support anonymous globs, "Symbol::ungensym" is also provided. But it
43 doesn't do anything.
44
45 "Symbol::geniosym" creates an anonymous IO handle. This can be
46 assigned into an existing glob without affecting the non-IO portions of
47 the glob.
48
49 "Symbol::qualify" turns unqualified symbol names into qualified
50 variable names (e.g. "myvar" -> "MyPackage::myvar"). If it is given a
51 second parameter, "qualify" uses it as the default package; otherwise,
52 it uses the package of its caller. Regardless, global variable names
53 (e.g. "STDOUT", "ENV", "SIG") are always qualified with "main::".
54
55 Qualification applies only to symbol names (strings). References are
56 left unchanged under the assumption that they are glob references,
57 which are qualified by their nature.
58
59 "Symbol::qualify_to_ref" is just like "Symbol::qualify" except that it
60 returns a glob ref rather than a symbol name, so you can use the result
61 even if "use strict 'refs'" is in effect.
62
63 "Symbol::delete_package" wipes out a whole package namespace. Note
64 this routine is not exported by default--you may want to import it
65 explicitly.
66
68 "Symbol::delete_package" is a bit too powerful. It undefines every
69 symbol that lives in the specified package. Since perl, for performance
70 reasons, does not perform a symbol table lookup each time a function is
71 called or a global variable is accessed, some code that has already
72 been loaded and that makes use of symbols in package "Foo" may stop
73 working after you delete "Foo", even if you reload the "Foo" module
74 afterwards.
75
76
77
78perl v5.32.1 2021-03-31 Symbol(3pm)