| 1 | #!/usr/bin/perl -c | ||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||
| 3 | package Symbol::Util; | ||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||
| 7 | Symbol::Util - Additional utils for Perl symbols manipulation | ||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||
| 11 | use Symbol::Util ':all'; | ||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | my $caller = caller; | ||||||||||||||||||||||||||||||||||||||||||||
| 14 | *{ fetch_glob("${caller}::foo") } = sub { "this is foo" }; | ||||||||||||||||||||||||||||||||||||||||||||
| 15 | my $coderef = fetch_glob("${caller}::bar", "CODE"); | ||||||||||||||||||||||||||||||||||||||||||||
| 16 | sub baz { 42; } | ||||||||||||||||||||||||||||||||||||||||||||
| 17 | export_glob($caller, "baz"); | ||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | print join "\n", keys %{ stash("main") }; | ||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | delete_glob("${caller}::foo", "CODE"); | ||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||
| 23 | use constant PI => 3.14159265; | ||||||||||||||||||||||||||||||||||||||||||||
| 24 | delete_sub "PI"; # remove constant from public API | ||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | require YAML; | ||||||||||||||||||||||||||||||||||||||||||||
| 27 | export_package(__PACKAGE__, "YAML", "Dump"); # import YAML::Dump | ||||||||||||||||||||||||||||||||||||||||||||
| 28 | unexport_package(__PACKAGE, "YAML"); # remove imported symbols | ||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | no Symbol::Util; # clean all symbols imported from Symbol::Util | ||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | This module provides a set of additional functions useful for Perl | ||||||||||||||||||||||||||||||||||||||||||||
| 35 | symbols manipulation. | ||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | All Perl symbols from the same package are organized as a stash. Each symbol | ||||||||||||||||||||||||||||||||||||||||||||
| 38 | (glob) contains one or more of following slots: C<SCALAR>, C<ARRAY>, C<HASH>, | ||||||||||||||||||||||||||||||||||||||||||||
| 39 | C<CODE>, C<IO>, C<FORMAT>. These slots are also accessible as standard | ||||||||||||||||||||||||||||||||||||||||||||
| 40 | variables or bare words. | ||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | The Perl symbols table is directly accessible with typeglob prefix but it can | ||||||||||||||||||||||||||||||||||||||||||||
| 43 | be difficult to read and problematic if strict mode is used. Also the access | ||||||||||||||||||||||||||||||||||||||||||||
| 44 | to stash, glob and one of its slot have different syntax notation. | ||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | C<stash> and C<fetch_glob> functions gets stash or glob without need to use | ||||||||||||||||||||||||||||||||||||||||||||
| 47 | C<no strict 'refs'>. | ||||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||||
| 49 | C<delete_glob> function allows to delete specific slot of | ||||||||||||||||||||||||||||||||||||||||||||
| 50 | symbol name without deleting others. | ||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||
| 52 | C<delete_sub> removes the symbol from class API. This symbol won't be | ||||||||||||||||||||||||||||||||||||||||||||
| 53 | available as an object method. | ||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | C<export_glob> function exports a glob to the target package. | ||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | C<export_package> works like L<Exporter> module and allows to export symbols | ||||||||||||||||||||||||||||||||||||||||||||
| 58 | from one package to other. | ||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | C<unexport_package> allows to delete previously exported symbols. | ||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | =for readme stop | ||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||
| 67 | 8 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||
| 69 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 70 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | 8 | our $VERSION = '0.0203'; | |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | # Exported symbols $EXPORTED{$target}{$package}{$name}{$slot} = 1 | ||||||||||||||||||||||||||||||||||||||||||||
| 76 | 8 | my %EXPORTED; | |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||
| 79 | =head1 USAGE | ||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | By default, the class does not export its symbols. | ||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | =over | ||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | =item use Symbol::Util ':all'; | ||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||
| 87 | Imports all available symbols. | ||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | =cut | ||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | 17 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 92 | 17 | my ($package, @names) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | 17 | my $caller = caller(); | |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | 17 | ▶ | my @EXPORT_OK = grep { /^[a-z]/ && !/^(?:import|unimport)$/ } keys %{ stash(__PACKAGE__) }; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
/^[a-z]/ && !/^(?:import|unimport)$/
MC/DC100%2/2
/^[a-z]/ && !/^(?:import|unimport)$/
/^[a-z]/
!/^(?:import|unimport)$/
Truth table100%3/3
A && not(B)
A= /^[a-z]/B= !/^(?:import|unimport)$/
| |||||||||||||||||||||||||||||||||||||||||||||
| 97 | 17 | my %EXPORT_TAGS = ( all => [ @EXPORT_OK ] ); | |||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||
| 99 | 17 | return export_package($caller, $package, { | |||||||||||||||||||||||||||||||||||||||||||
| 100 | OK => [ @EXPORT_OK ], | ||||||||||||||||||||||||||||||||||||||||||||
| 101 | TAGS => { %EXPORT_TAGS }, | ||||||||||||||||||||||||||||||||||||||||||||
| 102 | }, @names); | ||||||||||||||||||||||||||||||||||||||||||||
| 103 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||
| 106 | =item no Symbol::Util; | ||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||
| 108 | Deletes all imported symbols from caller name space. | ||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | =back | ||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | =cut | ||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||
| 114 | 9 | ▶ | sub unimport { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unimport: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 115 | 9 | my ($package) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | 9 | my $caller = caller(); | |||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||
| 119 | 9 | return unexport_package($caller, $package); | |||||||||||||||||||||||||||||||||||||||||||
| 120 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||
| 125 | =over | ||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | =item stash( I<name> : Str ) : HashRef | ||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | Returns a reference to the stash for the specified name. If the stash does | ||||||||||||||||||||||||||||||||||||||||||||
| 130 | not already exist then it will be created. The name of the stash does not | ||||||||||||||||||||||||||||||||||||||||||||
| 131 | include the C<::> at the end. It is safe to use this function with | ||||||||||||||||||||||||||||||||||||||||||||
| 132 | C<use strict 'refs'>. | ||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||
| 134 | print join "\n", keys %{ stash("main") }; | ||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||
| 138 | 61 | ▶ | sub stash ($) { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 139 | 8 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 140 | 61 | return *{ $_[0] . '::' }{HASH}; | |||||||||||||||||||||||||||||||||||||||||||
| 141 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||
| 144 | =item fetch_glob( I<name> : Str ) : GlobRef | ||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||
| 146 | =item fetch_glob( I<name> : Str, I<slot> : Str ) : Ref | ||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | Returns a reference to the glob for the specified symbol name. If the symbol | ||||||||||||||||||||||||||||||||||||||||||||
| 149 | does not already exist then it will be created. If the symbol name is | ||||||||||||||||||||||||||||||||||||||||||||
| 150 | unqualified then it will be looked up in the calling package. It is safe to | ||||||||||||||||||||||||||||||||||||||||||||
| 151 | use this function with C<use strict 'refs'>. | ||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||
| 153 | If the I<slot> argument is defined and this slot contains defined value, | ||||||||||||||||||||||||||||||||||||||||||||
| 154 | reference to this value is returned. The I<slot> argument can be one of the | ||||||||||||||||||||||||||||||||||||||||||||
| 155 | following strings: C<SCALAR>, C<ARRAY>, C<HASH>, C<CODE>, C<IO>, C<FORMAT>). | ||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||
| 157 | my $caller = caller; | ||||||||||||||||||||||||||||||||||||||||||||
| 158 | *{ fetch_glob("${caller}::foo") } = sub { "this is foo" }; | ||||||||||||||||||||||||||||||||||||||||||||
| 159 | my $coderef = fetch_glob("${caller}::foo", "CODE"); | ||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||
| 161 | =cut | ||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||
| 163 | 168 | ▶ | sub fetch_glob ($;$) { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fetch_glob: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 164 | 168 | my ($name, $slot) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||
| 166 | 168 | ▶ | $name = caller() . "::$name" unless $name =~ /::/; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||
| 168 | 8 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | 168 | ▶ | if (defined $slot) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 171 | 96 | ▶ | return if $slot eq 'SCALAR' and not defined ${ *{ $name }{SCALAR} }; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$slot eq "SCALAR" and not defined ${*{$name;}{"SCALAR"};}
MC/DC100%2/2
$slot eq "SCALAR" and not defined ${*{$name;}{"SCALAR"};}
$slot eq "SCALAR"
not defined ${*{$name;}{"SCALAR"};}
Truth table100%3/3
A and not(B)
A= $slot eq "SCALAR"B= not defined ${*{$name;}{"SCALAR"};}
| |||||||||||||||||||||||||||||||||||||||||||||
| 172 | 94 | return *{ $name }{$slot}; | |||||||||||||||||||||||||||||||||||||||||||
| 173 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||
| 175 | 72 | return \*{ $name }; | |||||||||||||||||||||||||||||||||||||||||||
| 176 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||
| 179 | =item list_glob_slots( I<name> ) : Maybe[Array] | ||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||
| 181 | Returns a list of slot names for glob with specified I<name> which contain | ||||||||||||||||||||||||||||||||||||||||||||
| 182 | defined value. If the glob is undefined, the C<undef> value is returned. If | ||||||||||||||||||||||||||||||||||||||||||||
| 183 | the glob is defined and has no defined slots, the empty list is returned. | ||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||
| 185 | The C<SCALAR> slot is used only if it contains defined value. | ||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||
| 187 | my $foo = 42; | ||||||||||||||||||||||||||||||||||||||||||||
| 188 | my @foo = (1, 2); | ||||||||||||||||||||||||||||||||||||||||||||
| 189 | sub foo { 1; }; | ||||||||||||||||||||||||||||||||||||||||||||
| 190 | print join ",", list_glob_slots("foo"); # SCALAR,ARRAY,CODE | ||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||
| 192 | =cut | ||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||
| 194 | 45 | ▶ | sub list_glob_slots ($) { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_glob_slots: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 195 | 45 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||
| 197 | 45 | ▶ | $name = caller() . "::$name" unless $name =~ /::/; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||
| 199 | 8 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||
| 201 | 45 | ▶ | return if not defined *{ $name }; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||
| 203 | 44 | my @slots; | |||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||
| 205 | push @slots, 'SCALAR' | ||||||||||||||||||||||||||||||||||||||||||||
| 206 | 44 | ▶ | if defined *{ $name }{SCALAR} and defined ${ *{ $name }{SCALAR} }; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined *{$name;}{"SCALAR"} and defined ${*{$name;}{"SCALAR"};}
MC/DC50%1/2
defined *{$name;}{"SCALAR"} and defined ${*{$name;}{"SCALAR"};}
defined *{$name;}{"SCALAR"}
defined ${*{$name;}{"SCALAR"};}
Truth table66%2/3
A and B
A= defined *{$name;}{"SCALAR"}B= defined ${*{$name;}{"SCALAR"};}
| |||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||
| 208 | 44 | foreach my $slot (qw( ARRAY HASH CODE IO )) { | |||||||||||||||||||||||||||||||||||||||||||
| 209 | 176 | ▶ | push @slots, $slot if defined *{ $name }{$slot}; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 210 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||
| 212 | 44 | return @slots; | |||||||||||||||||||||||||||||||||||||||||||
| 213 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||
| 216 | =item export_glob( I<target>, I<name> : Str ) : GlobRef | ||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | =item export_glob( I<target>, I<name> : Str, I<slots> : Array ) : Ref | ||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||
| 220 | Exports a glob I<name> to the I<target> package. Optionally exports only | ||||||||||||||||||||||||||||||||||||||||||||
| 221 | specified slots of the glob. | ||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||
| 223 | sub my_function { ... }; | ||||||||||||||||||||||||||||||||||||||||||||
| 224 | sub import { | ||||||||||||||||||||||||||||||||||||||||||||
| 225 | my $caller = caller; | ||||||||||||||||||||||||||||||||||||||||||||
| 226 | export_glob($caller, "my_function"); | ||||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||
| 229 | =cut | ||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||
| 231 | 48 | ▶ | sub export_glob ($$;@) { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
export_glob: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 232 | 48 | my ($target, $name, @slots) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||
| 234 | 48 | ▶ | $name = caller() . "::$name" unless $name =~ /::/; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 235 | 48 | (my $subname = $name) =~ s/^(.*):://; | |||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||
| 237 | 48 | ▶ | @slots = qw( SCALAR ARRAY HASH CODE IO ) unless @slots; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||
| 239 | 8 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||
| 241 | 48 | ▶ | return if not defined *{ $name }; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||
| 243 | 47 | my $targetname = "${target}::$subname"; | |||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||
| 245 | 47 | my $defined; | |||||||||||||||||||||||||||||||||||||||||||
| 246 | 47 | foreach my $slot (@slots) { | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 51 | ▶ | next if $slot eq 'SCALAR' and not defined ${ *{ $name }{$slot} }; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$slot eq "SCALAR" and not defined ${*{$name;}{$slot};}
MC/DC100%2/2
$slot eq "SCALAR" and not defined ${*{$name;}{$slot};}
$slot eq "SCALAR"
not defined ${*{$name;}{$slot};}
Truth table100%3/3
A and not(B)
A= $slot eq "SCALAR"B= not defined ${*{$name;}{$slot};}
| |||||||||||||||||||||||||||||||||||||||||||||
| 248 | 50 | ▶ | next if not defined *{ $name }{$slot}; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 249 | 46 | *{ $targetname } = *{ $name }{$slot}; | |||||||||||||||||||||||||||||||||||||||||||
| 250 | 46 | $defined = 1; | |||||||||||||||||||||||||||||||||||||||||||
| 251 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||
| 253 | 47 | ▶ | return $defined ? \*{ $targetname } : undef; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 254 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||
| 257 | =item delete_glob( I<name> : Str, I<slots> : Array[Str] ) : Maybe[GlobRef] | ||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||
| 259 | Deletes the specified symbol name if I<slots> are not specified, or deletes | ||||||||||||||||||||||||||||||||||||||||||||
| 260 | the specified slots in the symbol name (could be one or more of the following | ||||||||||||||||||||||||||||||||||||||||||||
| 261 | strings: C<SCALAR>, C<ARRAY>, C<HASH>, C<CODE>, C<IO>, C<FORMAT>). | ||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||
| 263 | Function returns the glob reference if there are any slots defined. | ||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||
| 265 | our $FOO = 1; | ||||||||||||||||||||||||||||||||||||||||||||
| 266 | sub FOO { "bar" }; | ||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||
| 268 | delete_glob("FOO", "CODE"); | ||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||
| 270 | print $FOO; # prints "1" | ||||||||||||||||||||||||||||||||||||||||||||
| 271 | FOO(); # error: sub not found | ||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||
| 273 | =cut | ||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||
| 275 | 12 | ▶ | sub delete_glob ($;@) { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_glob: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 276 | 12 | my ($name, @slots) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||
| 278 | 12 | ▶ | $name = caller() . "::$name" unless $name =~ /::/; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 279 | 12 | $name =~ /^(.*)::([^:]*)/; | |||||||||||||||||||||||||||||||||||||||||||
| 280 | 12 | my ($package, $subname) = ($1, $2); ## no critic qw(ProhibitCaptureWithoutTest) | |||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||||
| 282 | 12 | my $stash = stash($package); | |||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||
| 284 | 12 | ▶ | if (@slots) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 285 | 10 | my %delete = map { $_ => 1 } @slots; | |||||||||||||||||||||||||||||||||||||||||||
| 286 | 10 | my %backup; | |||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||
| 288 | 10 | foreach my $slot (list_glob_slots($name)) { | |||||||||||||||||||||||||||||||||||||||||||
| 289 | $backup{$slot} = fetch_glob($name, $slot) | ||||||||||||||||||||||||||||||||||||||||||||
| 290 | 33 | ▶ | if not $delete{$slot}; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 291 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||
| 293 | 10 | undef $stash->{$subname}; | |||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||
| 295 | 10 | foreach my $slot (keys %backup) { | |||||||||||||||||||||||||||||||||||||||||||
| 296 | 22 | *{ fetch_glob($name) } = $backup{$slot}; | |||||||||||||||||||||||||||||||||||||||||||
| 297 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||
| 299 | 10 | return fetch_glob($name); | |||||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 301 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 302 | # delete all slots | ||||||||||||||||||||||||||||||||||||||||||||
| 303 | 2 | undef $stash->{$subname}; | |||||||||||||||||||||||||||||||||||||||||||
| 304 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||
| 306 | 2 | return; | |||||||||||||||||||||||||||||||||||||||||||
| 307 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||
| 310 | =item delete_sub( I<name> : Str ) : Maybe[GlobRef] | ||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||
| 312 | Deletes (or hides) the specified subroutine name from class API. It means | ||||||||||||||||||||||||||||||||||||||||||||
| 313 | that this subroutine will be no longer available as a class method. The | ||||||||||||||||||||||||||||||||||||||||||||
| 314 | purpose of this function is the same as L<namespace::clean> pragma has: it | ||||||||||||||||||||||||||||||||||||||||||||
| 315 | cleans a package's namespace from unwanted subroutines. Function doesn't | ||||||||||||||||||||||||||||||||||||||||||||
| 316 | delete other slots than C<CODE> slot of the glob. | ||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||
| 318 | Function returns the glob reference if there are any other slots still defined | ||||||||||||||||||||||||||||||||||||||||||||
| 319 | than <CODE> slot. | ||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||
| 321 | package My::Class; | ||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||
| 323 | use constant PI => 3.14159265; | ||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||
| 325 | use Symbol::Util 'delete_sub'; | ||||||||||||||||||||||||||||||||||||||||||||
| 326 | delete_sub "PI"; # remove constant from public API | ||||||||||||||||||||||||||||||||||||||||||||
| 327 | no Symbol::Util; # remove also Symbol::Util::* from public API | ||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||
| 329 | sub area { | ||||||||||||||||||||||||||||||||||||||||||||
| 330 | my ($self, $r) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
| 331 | return PI * $r ** 2; | ||||||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||
| 334 | print My::Class->area(2); # prints 12.5663706 | ||||||||||||||||||||||||||||||||||||||||||||
| 335 | print My::Class->PI; # Can't locate object method | ||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||
| 337 | =cut | ||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||
| 339 | 31 | ▶ | sub delete_sub ($) { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_sub: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 340 | 31 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||||
| 342 | 31 | ▶ | $name = caller() . "::$name" unless $name =~ /::/; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 343 | 31 | $name =~ /^(.*)::([^:]*)/; | |||||||||||||||||||||||||||||||||||||||||||
| 344 | 31 | my ($package, $subname) = ($1, $2); ## no critic qw(ProhibitCaptureWithoutTest) | |||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||
| 346 | 31 | ▶ | return if not defined fetch_glob($name, 'CODE'); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||
| 348 | 30 | my $stash = stash($package); | |||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||
| 350 | 30 | my %backup; | |||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||
| 352 | 30 | foreach my $slot (list_glob_slots($name)) { | |||||||||||||||||||||||||||||||||||||||||||
| 353 | 34 | $backup{$slot} = fetch_glob($name, $slot); | |||||||||||||||||||||||||||||||||||||||||||
| 354 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||
| 356 | 30 | *{ fetch_glob($name) } = $backup{CODE}; | |||||||||||||||||||||||||||||||||||||||||||
| 357 | 30 | delete $backup{CODE}; | |||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||
| 359 | 30 | delete $stash->{$subname}; | |||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||||
| 361 | 30 | foreach my $slot (keys %backup) { | |||||||||||||||||||||||||||||||||||||||||||
| 362 | 4 | *{ fetch_glob($name) } = $backup{$slot}; | |||||||||||||||||||||||||||||||||||||||||||
| 363 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||
| 365 | 30 | ▶ | return %backup ? fetch_glob($name) : undef; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 366 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||||
| 369 | =item export_package( I<target> : Str, I<package> : Str, I<names> : Array[Str] ) : Bool | ||||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||||
| 371 | =item export_package( I<target> : Str, I<package> : Str, I<spec> : HashRef, I<names> : Array[Str] ) : Bool | ||||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||||
| 373 | Exports symbols from I<package> to I<target>. If I<spec> is defined as hash | ||||||||||||||||||||||||||||||||||||||||||||
| 374 | reference, it contains the specification for exporter. Otherwise the standard | ||||||||||||||||||||||||||||||||||||||||||||
| 375 | global variables of I<package> are used (C<@EXPORT>, C<@EXPORT_OK> and | ||||||||||||||||||||||||||||||||||||||||||||
| 376 | C<%EXPORT_TAGS>) to build the specification for exporter. The optional list | ||||||||||||||||||||||||||||||||||||||||||||
| 377 | of I<names> defines an import list. | ||||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||||
| 379 | The I<spec> is a reference to hash with following keys: | ||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||
| 381 | =over | ||||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||||
| 383 | =item EXPORT | ||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||
| 385 | Contains the list of default imports. It is the same as C<@EXPORT> variable. | ||||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||||
| 387 | =item OK | ||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||
| 389 | Contains the list of allowed imports. It is the same as C<@EXPORT_OK> | ||||||||||||||||||||||||||||||||||||||||||||
| 390 | variable. | ||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||
| 392 | =item TAGS | ||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||
| 394 | Contains the hash with tags. It is the same as C<%EXPORT_TAGS> variable. | ||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||
| 396 | =back | ||||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||||
| 398 | See L<Exporter> documentation for explanation of these global variables and | ||||||||||||||||||||||||||||||||||||||||||||
| 399 | list of I<names>. | ||||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||||
| 401 | The C<export_package> function can export symbols from an external package to | ||||||||||||||||||||||||||||||||||||||||||||
| 402 | an external package. This function can also be used as a helper in C<import> | ||||||||||||||||||||||||||||||||||||||||||||
| 403 | method. | ||||||||||||||||||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||||||||||||||||||
| 405 | package My::Package; | ||||||||||||||||||||||||||||||||||||||||||||
| 406 | sub myfunc { }; | ||||||||||||||||||||||||||||||||||||||||||||
| 407 | sub import { | ||||||||||||||||||||||||||||||||||||||||||||
| 408 | my ($package, @names) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
| 409 | my $caller = caller(); | ||||||||||||||||||||||||||||||||||||||||||||
| 410 | return export_package($caller, $package, { | ||||||||||||||||||||||||||||||||||||||||||||
| 411 | OK => [ qw( myfunc ) ], | ||||||||||||||||||||||||||||||||||||||||||||
| 412 | }, @names); | ||||||||||||||||||||||||||||||||||||||||||||
| 413 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||||
| 415 | All exported symbols are tracked and later can be removed with | ||||||||||||||||||||||||||||||||||||||||||||
| 416 | C<unexport_package> function. | ||||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||||
| 418 | The function returns true value if there were no errors. | ||||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||||
| 420 | =cut | ||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||
| 422 | 33 | ▶ | sub export_package ($$@) { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
export_package: called CC=33 SCAR=35.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 423 | 33 | my ($target, $package, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||
| 425 | 33 | ▶ | my $spec = ref $args[0] eq 'HASH' ? shift @args : { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 426 | EXPORT => fetch_glob("${package}::EXPORT", "ARRAY"), | ||||||||||||||||||||||||||||||||||||||||||||
| 427 | OK => fetch_glob("${package}::EXPORT_OK", "ARRAY"), | ||||||||||||||||||||||||||||||||||||||||||||
| 428 | TAGS => fetch_glob("${package}::EXPORT_TAGS", "HASH"), | ||||||||||||||||||||||||||||||||||||||||||||
| 429 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||||
| 431 | 33 | my @names = @args; | |||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||
| 433 | # support: use Package 3.14 qw(); | ||||||||||||||||||||||||||||||||||||||||||||
| 434 | 33 | ▶ | return 1 if @names == 1 and $names[0] eq ''; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@names == 1 and $names[0] eq ""
MC/DC100%2/2
@names == 1 and $names[0] eq ""
@names == 1
$names[0] eq ""
Truth table100%3/3
A and B
A= @names == 1B= $names[0] eq ""
| |||||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||||
| 436 | # default exports on empty list or if first element is negation | ||||||||||||||||||||||||||||||||||||||||||||
| 437 | 32 | ▶ | unshift @names, ":DEFAULT" if not @names or @names and $names[0] =~ /^!/; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not @names or @names and $names[0] =~ /^!/
@names and $names[0] =~ /^!/
MC/DC66%2/3
not @names or @names and $names[0] =~ /^!/
not @names
@names
$names[0] =~ /^!/
Truth table75%3/4
not(A) or B and C
A= not @namesB= @namesC= $names[0] =~ /^!/
| |||||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||||
| 439 | 32 | ▶ | my @export = ref ($spec->{EXPORT} || '') eq 'ARRAY' ? @{ $spec->{EXPORT} } : (); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$spec->{'EXPORT'} || ''
MC/DC100%1/1
$spec->{'EXPORT'} || ''
$spec->{'EXPORT'}
Truth table100%2/2
A
A= $spec->{'EXPORT'}
| |||||||||||||||||||||||||||||||||||||||||||||
| 440 | 32 | ▶ | my @export_ok = ref ($spec->{OK} || '') eq 'ARRAY' ? @{ $spec->{OK} } : (); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$spec->{'OK'} || ''
MC/DC100%1/1
$spec->{'OK'} || ''
$spec->{'OK'}
Truth table100%2/2
A
A= $spec->{'OK'}
| |||||||||||||||||||||||||||||||||||||||||||||
| 441 | 32 | ▶ | my %export_tags = ref ($spec->{TAGS} || '') eq 'HASH' ? %{ $spec->{TAGS} } : (); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$spec->{'TAGS'} || ''
MC/DC100%1/1
$spec->{'TAGS'} || ''
$spec->{'TAGS'}
Truth table100%2/2
A
A= $spec->{'TAGS'}
| |||||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||||
| 443 | 32 | my %export = map { $_ => 1 } @export; | |||||||||||||||||||||||||||||||||||||||||||
| 444 | 32 | my %export_ok = map { $_ => 1 } @export_ok; | |||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||
| 446 | 32 | my %names; | |||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||||
| 448 | 32 | ▶ | while (my $name = shift @names) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 449 | 62 | ▶ | if ($name =~ m{^/(.*)/$}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||||
| 450 | 1 | my $pattern = $1; | |||||||||||||||||||||||||||||||||||||||||||
| 451 | 1 | $names{$_} = 1 foreach grep { /$pattern/ } (@export, @export_ok); | |||||||||||||||||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 453 | elsif ($name =~ m{^!/(.*)/$}) { | ||||||||||||||||||||||||||||||||||||||||||||
| 454 | 1 | my $pattern = $1; | |||||||||||||||||||||||||||||||||||||||||||
| 455 | 1 | %names = map { $_ => 1 } grep { ! /$pattern/ } keys %names; | |||||||||||||||||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 457 | elsif ($name =~ /^(!?):DEFAULT$/) { | ||||||||||||||||||||||||||||||||||||||||||||
| 458 | 6 | my $neg = $1; | |||||||||||||||||||||||||||||||||||||||||||
| 459 | 6 | unshift @names, map { "${neg}$_" } @export; | |||||||||||||||||||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 461 | elsif ($name =~ /^(!?):(.*)$/) { | ||||||||||||||||||||||||||||||||||||||||||||
| 462 | 6 | my ($neg, $tag) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||
| 463 | 6 | ▶ | if (defined $export_tags{$tag}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 464 | 5 | unshift @names, map { "${neg}$_" } @{ $export_tags{$tag} }; | |||||||||||||||||||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 466 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 467 | 1 | require Carp; | |||||||||||||||||||||||||||||||||||||||||||
| 468 | 1 | Carp::croak("$name is not a tag of the $package module"); | |||||||||||||||||||||||||||||||||||||||||||
| 469 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 471 | elsif ($name =~ /^!(.*)$/) { | ||||||||||||||||||||||||||||||||||||||||||||
| 472 | 2 | $name = $1; | |||||||||||||||||||||||||||||||||||||||||||
| 473 | 2 | delete $names{$name}; | |||||||||||||||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 475 | ▶ | elsif (defined $export_ok{$name} or defined $export{$name}) { | |||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
defined $export_ok{$name} or defined $export{$name}
MC/DC100%2/2
defined $export_ok{$name} or defined $export{$name}
defined $export_ok{$name}
defined $export{$name}
Truth table100%3/3
A or B
A= defined $export_ok{$name}B= defined $export{$name}
| |||||||||||||||||||||||||||||||||||||||||||||
| 476 | 44 | $names{$name} = 1; | |||||||||||||||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 478 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 479 | 2 | require Carp; | |||||||||||||||||||||||||||||||||||||||||||
| 480 | 2 | Carp::croak("$name is not exported by the $package module"); | |||||||||||||||||||||||||||||||||||||||||||
| 481 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 482 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||||
| 484 | 29 | foreach my $name (keys %names) { | |||||||||||||||||||||||||||||||||||||||||||
| 485 | 42 | my $type = ''; | |||||||||||||||||||||||||||||||||||||||||||
| 486 | 42 | ▶ | if ($name =~ s/^(\W)//) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 487 | 6 | $type = $1; | |||||||||||||||||||||||||||||||||||||||||||
| 488 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||||
| 490 | 42 | my @slots; | |||||||||||||||||||||||||||||||||||||||||||
| 491 | 42 | ▶ | if ($type eq '&' or $type eq '') { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
Condition100%3/3
$type eq "&" or $type eq ""
MC/DC100%2/2
$type eq "&" or $type eq ""
$type eq "&"
$type eq ""
Truth table100%3/3
A or B
A= $type eq "&"B= $type eq ""
| |||||||||||||||||||||||||||||||||||||||||||||
| 492 | 37 | push @slots, 'CODE'; | |||||||||||||||||||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 494 | elsif ($type eq '$') { | ||||||||||||||||||||||||||||||||||||||||||||
| 495 | 1 | push @slots, 'SCALAR'; | |||||||||||||||||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 497 | elsif ($type eq '@') { | ||||||||||||||||||||||||||||||||||||||||||||
| 498 | 1 | push @slots, 'ARRAY'; | |||||||||||||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 500 | elsif ($type eq '%') { | ||||||||||||||||||||||||||||||||||||||||||||
| 501 | 1 | push @slots, 'HASH'; | |||||||||||||||||||||||||||||||||||||||||||
| 502 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 503 | elsif ($type eq '*') { | ||||||||||||||||||||||||||||||||||||||||||||
| 504 | 1 | push @slots, 'IO'; | |||||||||||||||||||||||||||||||||||||||||||
| 505 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 506 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 507 | 1 | require Carp; | |||||||||||||||||||||||||||||||||||||||||||
| 508 | 1 | Carp::croak("Can't export symbol $type$name"); | |||||||||||||||||||||||||||||||||||||||||||
| 509 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 510 | 41 | foreach my $slot (@slots) { | |||||||||||||||||||||||||||||||||||||||||||
| 511 | 41 | ▶ | if (defined export_glob($target, "${package}::$name", $slot)) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 512 | 41 | $EXPORTED{$target}{$package}{$name}{$slot} = 1; | |||||||||||||||||||||||||||||||||||||||||||
| 513 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 514 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 515 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||||
| 517 | 28 | return 1; | |||||||||||||||||||||||||||||||||||||||||||
| 518 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||||
| 521 | =item unexport_package( I<target> : Str, I<package> : Str ) : Bool | ||||||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||||
| 523 | Deletes symbols previously exported from I<package> to I<target> with | ||||||||||||||||||||||||||||||||||||||||||||
| 524 | C<export_package> function. If the symbol was C<CODE> reference it is deleted | ||||||||||||||||||||||||||||||||||||||||||||
| 525 | with C<delete_sub> function. Otherwise it is deleted with C<delete_glob> | ||||||||||||||||||||||||||||||||||||||||||||
| 526 | function with proper slot as an argument. | ||||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||||
| 528 | Deleting with C<delete_sub> function means that this symbol is not available | ||||||||||||||||||||||||||||||||||||||||||||
| 529 | via class API as an object method. | ||||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||||
| 531 | require YAML; | ||||||||||||||||||||||||||||||||||||||||||||
| 532 | export_package(__PACKAGE__, "YAML", "Dump"); | ||||||||||||||||||||||||||||||||||||||||||||
| 533 | unexport_package(__PACKAGE__, "YAML"); | ||||||||||||||||||||||||||||||||||||||||||||
| 534 | print Dump @INC; # OK | ||||||||||||||||||||||||||||||||||||||||||||
| 535 | __PACKAGE__->Dump; # Can't locate object method | ||||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||||
| 537 | This function can be used as a helper in C<unimport> method. | ||||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||||
| 539 | package My::Package; | ||||||||||||||||||||||||||||||||||||||||||||
| 540 | sub unimport { | ||||||||||||||||||||||||||||||||||||||||||||
| 541 | my ($package, @names) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
| 542 | my $caller = caller(); | ||||||||||||||||||||||||||||||||||||||||||||
| 543 | return unexport_package($caller, $package); | ||||||||||||||||||||||||||||||||||||||||||||
| 544 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||||
| 546 | package main; | ||||||||||||||||||||||||||||||||||||||||||||
| 547 | use My::Package qw(something); | ||||||||||||||||||||||||||||||||||||||||||||
| 548 | no My::Package; | ||||||||||||||||||||||||||||||||||||||||||||
| 549 | main->something; # Can't locate object method | ||||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||||
| 551 | The function returns true value if there were no errors. | ||||||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||||
| 553 | =back | ||||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||||
| 555 | =cut | ||||||||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||||||||
| 557 | 19 | ▶ | sub unexport_package ($$) { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unexport_package: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 558 | 19 | my ($target, $package) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||||
| 560 | 19 | ▶ | if (defined $EXPORTED{$target}{$package}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 561 | 19 | foreach my $name (keys %{ $EXPORTED{$target}{$package} }) { | |||||||||||||||||||||||||||||||||||||||||||
| 562 | # CODE slot have to be the last one | ||||||||||||||||||||||||||||||||||||||||||||
| 563 | 29 | foreach my $slot ( qw( SCALAR ARRAY HASH IO CODE ) ) { | |||||||||||||||||||||||||||||||||||||||||||
| 564 | 145 | ▶ | next unless exists $EXPORTED{$target}{$package}{$name}{$slot}; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 565 | 32 | ▶ | if ($slot eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 566 | 28 | delete_sub("${target}::$name"); | |||||||||||||||||||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 568 | else { | ||||||||||||||||||||||||||||||||||||||||||||
| 569 | 4 | delete_glob("${target}::$name", $slot); | |||||||||||||||||||||||||||||||||||||||||||
| 570 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 571 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 572 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 573 | 19 | delete $EXPORTED{$target}{$package}; | |||||||||||||||||||||||||||||||||||||||||||
| 574 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||||
| 576 | 19 | return 1; | |||||||||||||||||||||||||||||||||||||||||||
| 577 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||||
| 580 | 8 | 1; | |||||||||||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||||
| 583 | =begin umlwiki | ||||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||||
| 585 | = Class Diagram = | ||||||||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||||||||
| 587 | [ <<utility>> | ||||||||||||||||||||||||||||||||||||||||||||
| 588 | Symbol::Util | ||||||||||||||||||||||||||||||||||||||||||||
| 589 | ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||
| 590 | ------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||
| 591 | stash( name : Str ) : HashRef | ||||||||||||||||||||||||||||||||||||||||||||
| 592 | fetch_glob( name : Str ) : GlobRef | ||||||||||||||||||||||||||||||||||||||||||||
| 593 | fetch_glob( name : Str, slot : Str ) : Ref | ||||||||||||||||||||||||||||||||||||||||||||
| 594 | list_glob_slots( name : Str ) : Array | ||||||||||||||||||||||||||||||||||||||||||||
| 595 | export_glob( package : Str, name : Str ) : GlobRef | ||||||||||||||||||||||||||||||||||||||||||||
| 596 | export_glob( package : Str, name : Str, slots : Array[Str] ) : GlobRef | ||||||||||||||||||||||||||||||||||||||||||||
| 597 | delete_glob( name : Str, slots : Array[Str] ) : GlobRef | ||||||||||||||||||||||||||||||||||||||||||||
| 598 | delete_sub( name : Str ) : GlobRef | ||||||||||||||||||||||||||||||||||||||||||||
| 599 | export_package( target : Str, package : Str, names : Array[Str] ) : Bool | ||||||||||||||||||||||||||||||||||||||||||||
| 600 | export_package( target : Str, package : Str, spec : HashRef, names : Array[Str] ) : Bool | ||||||||||||||||||||||||||||||||||||||||||||
| 601 | unexport_package( target : Str, package : Str ) : Bool | ||||||||||||||||||||||||||||||||||||||||||||
| 602 | ] | ||||||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||||
| 604 | =end umlwiki | ||||||||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||||||||
| 606 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||||||||||||||
| 608 | L<Symbol>, L<Sub::Delete>, L<namespace::clean>, L<Exporter>. | ||||||||||||||||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||||||||||||||||
| 610 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||||||||||||||
| 612 | C<fetch_glob> returns C<undef> value if C<SCALAR> slot contains C<undef> value. | ||||||||||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||||||||||
| 614 | C<delete_glob> and C<delete_sub> delete C<SCALAR> slot if it exists and | ||||||||||||||||||||||||||||||||||||||||||||
| 615 | contains C<undef> value. | ||||||||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||||||||
| 617 | C<delete_glob> and C<delete_sub> always delete C<FORMAT> slot. | ||||||||||||||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||||||||||||||
| 619 | If you find the bug or want to implement new features, please report it at | ||||||||||||||||||||||||||||||||||||||||||||
| 620 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Symbol-Util> | ||||||||||||||||||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||||||||||||||||||
| 622 | =for readme continue | ||||||||||||||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||||||||||||||
| 624 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||||||||||||||||||
| 626 | Piotr Roszatycki <dexter@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||||
| 628 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||||||||||||
| 630 | Copyright (c) 2009, 2012 Piotr Roszatycki <dexter@cpan.org>. | ||||||||||||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||||||||||||
| 632 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||
| 633 | the same terms as perl itself. | ||||||||||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||||||||||
| 635 | See L<http://dev.perl.org/licenses/artistic.html> | ||||||||||||||||||||||||||||||||||||||||||||