| 1 | package Symbol; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | Symbol - manipulate Perl symbols and their names | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | use Symbol; | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | $sym = gensym; | ||||||||||||||||||||||||||||||||||||||||||
| 15 | open($sym, '<', "filename"); | ||||||||||||||||||||||||||||||||||||||||||
| 16 | $_ = <$sym>; | ||||||||||||||||||||||||||||||||||||||||||
| 17 | # etc. | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | ungensym $sym; # no effect | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | # replace *FOO{IO} handle but not $FOO, %FOO, etc. | ||||||||||||||||||||||||||||||||||||||||||
| 22 | *FOO = geniosym; | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | print qualify("x"), "\n"; # "main::x" | ||||||||||||||||||||||||||||||||||||||||||
| 25 | print qualify("x", "FOO"), "\n"; # "FOO::x" | ||||||||||||||||||||||||||||||||||||||||||
| 26 | print qualify("BAR::x"), "\n"; # "BAR::x" | ||||||||||||||||||||||||||||||||||||||||||
| 27 | print qualify("BAR::x", "FOO"), "\n"; # "BAR::x" | ||||||||||||||||||||||||||||||||||||||||||
| 28 | print qualify("STDOUT", "FOO"), "\n"; # "main::STDOUT" (global) | ||||||||||||||||||||||||||||||||||||||||||
| 29 | print qualify(\*x), "\n"; # returns \*x | ||||||||||||||||||||||||||||||||||||||||||
| 30 | print qualify(\*x, "FOO"), "\n"; # returns \*x | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | use strict refs; | ||||||||||||||||||||||||||||||||||||||||||
| 33 | print { qualify_to_ref $fh } "foo!\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 34 | $ref = qualify_to_ref $name, $pkg; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | use Symbol qw(delete_package); | ||||||||||||||||||||||||||||||||||||||||||
| 37 | delete_package('Foo::Bar'); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | print "deleted\n" unless exists $Foo::{'Bar::'}; | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | C<Symbol::gensym> creates an anonymous glob and returns a reference | ||||||||||||||||||||||||||||||||||||||||||
| 43 | to it. Such a glob reference can be used as a file or directory | ||||||||||||||||||||||||||||||||||||||||||
| 44 | handle. | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | For backward compatibility with older implementations that didn't | ||||||||||||||||||||||||||||||||||||||||||
| 47 | support anonymous globs, C<Symbol::ungensym> is also provided. | ||||||||||||||||||||||||||||||||||||||||||
| 48 | But it doesn't do anything. | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | C<Symbol::geniosym> creates an anonymous IO handle. This can be | ||||||||||||||||||||||||||||||||||||||||||
| 51 | assigned into an existing glob without affecting the non-IO portions | ||||||||||||||||||||||||||||||||||||||||||
| 52 | of the glob. | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | C<Symbol::qualify> turns unqualified symbol names into qualified | ||||||||||||||||||||||||||||||||||||||||||
| 55 | variable names (e.g. "myvar" -E<gt> "MyPackage::myvar"). If it is given a | ||||||||||||||||||||||||||||||||||||||||||
| 56 | second parameter, C<qualify> uses it as the default package; | ||||||||||||||||||||||||||||||||||||||||||
| 57 | otherwise, it uses the package of its caller. Regardless, global | ||||||||||||||||||||||||||||||||||||||||||
| 58 | variable names (e.g. "STDOUT", "ENV", "SIG") are always qualified with | ||||||||||||||||||||||||||||||||||||||||||
| 59 | "main::". | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | Qualification applies only to symbol names (strings). References are | ||||||||||||||||||||||||||||||||||||||||||
| 62 | left unchanged under the assumption that they are glob references, | ||||||||||||||||||||||||||||||||||||||||||
| 63 | which are qualified by their nature. | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | C<Symbol::qualify_to_ref> is just like C<Symbol::qualify> except that it | ||||||||||||||||||||||||||||||||||||||||||
| 66 | returns a glob ref rather than a symbol name, so you can use the result | ||||||||||||||||||||||||||||||||||||||||||
| 67 | even if C<use strict 'refs'> is in effect. | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | C<Symbol::delete_package> wipes out a whole package namespace. Note | ||||||||||||||||||||||||||||||||||||||||||
| 70 | this routine is not exported by default--you may want to import it | ||||||||||||||||||||||||||||||||||||||||||
| 71 | explicitly. | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | C<Symbol::delete_package> is a bit too powerful. It undefines every symbol that | ||||||||||||||||||||||||||||||||||||||||||
| 76 | lives in the specified package. Since perl, for performance reasons, does not | ||||||||||||||||||||||||||||||||||||||||||
| 77 | perform a symbol table lookup each time a function is called or a global | ||||||||||||||||||||||||||||||||||||||||||
| 78 | variable is accessed, some code that has already been loaded and that makes use | ||||||||||||||||||||||||||||||||||||||||||
| 79 | of symbols in package C<Foo> may stop working after you delete C<Foo>, even if | ||||||||||||||||||||||||||||||||||||||||||
| 80 | you reload the C<Foo> module afterwards. | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 7 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 7 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 86 | 7 | our @EXPORT = qw(gensym ungensym qualify qualify_to_ref); | |||||||||||||||||||||||||||||||||||||||||
| 87 | 7 | our @EXPORT_OK = qw(delete_package geniosym); | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 7 | our $VERSION = '1.09'; | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 7 | my $genpkg = "Symbol::"; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 7 | my $genseq = 0; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 7 | my %global = map {$_ => 1} qw(ARGV ARGVOUT ENV INC SIG STDERR STDIN STDOUT); | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | # | ||||||||||||||||||||||||||||||||||||||||||
| 97 | # Note that we never _copy_ the glob; we just make a ref to it. | ||||||||||||||||||||||||||||||||||||||||||
| 98 | # If we did copy it, then SVf_FAKE would be set on the copy, and | ||||||||||||||||||||||||||||||||||||||||||
| 99 | # glob-specific behaviors (e.g. C<*$ref = \&func>) wouldn't work. | ||||||||||||||||||||||||||||||||||||||||||
| 100 | # | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub gensym () { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
gensym: not called CC=1 SCAR=2.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my $name = "GEN" . $genseq++; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 7 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my $ref = \*{$genpkg . $name}; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | delete $$genpkg{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | $ref; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub geniosym () { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
geniosym: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | my $sym = gensym(); | |||||||||||||||||||||||||||||||||||||||||
| 111 | # force the IO slot to be filled | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | select(select $sym); | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | *$sym{IO}; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub ungensym ($) {} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ungensym: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub qualify ($;$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
qualify: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if (!ref($name) && index($name, '::') == -1 && index($name, "'") == -1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not ref $name and index($name, "::") == -1 and index($name, "'") == -1
not ref $name and index($name, "::") == -1
MC/DC0%0/3
not ref $name and index($name, "::") == -1 and index($name, "'") == -1
not ref $name
index($name, "::") == -1
index($name, "'") == -1
Truth table0%0/4
not(A) and B and C
A= not ref $nameB= index($name, "::") == -1C= index($name, "'") == -1
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my $pkg; | |||||||||||||||||||||||||||||||||||||||||
| 122 | # Global names: special character, "^xyz", or other. | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if ($name =~ /^(([^a-z])|(\^[a-z_]+))\z/i || $global{$name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$name =~ /^(([^a-z])|(\^[a-z_]+))\z/i or $global{$name}
MC/DC0%0/2
$name =~ /^(([^a-z])|(\^[a-z_]+))\z/i or $global{$name}
$name =~ /^(([^a-z])|(\^[a-z_]+))\z/i
$global{$name}
Truth table0%0/3
A or B
A= $name =~ /^(([^a-z])|(\^[a-z_]+))\z/iB= $global{$name}
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | # RGS 2001-11-05 : translate leading ^X to control-char | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | $name =~ s/^\^([a-z_])/'qq(\c'.$1.')'/eei; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $pkg = "main"; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | $pkg = (@_ > 1) ? $_[1] : caller; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | $name = $pkg . "::" . $name; | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | $name; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | sub qualify_to_ref ($;$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
qualify_to_ref: not called CC=2 SCAR=10.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 7 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | return \*{ qualify $_[0], @_ > 1 ? $_[1] : caller }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | # | ||||||||||||||||||||||||||||||||||||||||||
| 142 | # of Safe.pm lineage | ||||||||||||||||||||||||||||||||||||||||||
| 143 | # | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | sub delete_package ($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_package: not called CC=8 SCAR=37.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | my $pkg = shift; | |||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | # expand to full symbol table name if needed | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | ▶ | unless ($pkg =~ /^main::.*::$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | $pkg = "main$pkg" if $pkg =~ /^::/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | $pkg = "main::$pkg" unless $pkg =~ /^main::/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | $pkg .= '::' unless $pkg =~ /::$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | my($stem, $leaf) = $pkg =~ m/(.*::)(\w+::)$/; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 7 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | my $stem_symtab = *{$stem}{HASH}; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | return unless defined $stem_symtab and exists $stem_symtab->{$leaf}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $stem_symtab and exists $stem_symtab->{$leaf}
MC/DC0%0/2
defined $stem_symtab and exists $stem_symtab->{$leaf}
defined $stem_symtab
exists $stem_symtab->{$leaf}
Truth table0%0/3
A and B
A= defined $stem_symtabB= exists $stem_symtab->{$leaf}
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | # free all the symbols in the package | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | my $leaf_symtab = *{$stem_symtab->{$leaf}}{HASH}; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | foreach my $name (keys %$leaf_symtab) { | |||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | undef *{$pkg . $name}; | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 7 | ▶ | use strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | # delete the symbol table | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | %$leaf_symtab = (); | |||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | delete $stem_symtab->{$leaf}; | |||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||