File Coverage

blib/lib/Sub/Delete.pm
Criterion Covered Total %
statement 36 36 100.0
branch 11 16 68.7
condition 17 25 68.0
subroutine 7 7 100.0
pod 2 2 100.0
total 73 86 84.8


line stmt bran cond sub pod time code
1             ## no critic: TestingAndDebugging::RequireUseStrict
2 2     2   349185 use 5.008003;
  2         8  
3              
4             package
5             Sub::Delete; # hide from PAUSE indexing
6              
7             $VERSION = '1.00002';
8             @EXPORT = delete_sub;
9              
10 2     2   13 use Exporter 5.57 'import';
  2         50  
  2         154  
11 2     2   17 use constant point0 => 0+$] eq 5.01;
  2         3  
  2         274  
12              
13             # This sub must come before any lexical vars.
14             sub strict_eval($) {
15 19     19 1 22 local %^H if point0;
16 19         46 local *@;
17             use#
18 2     2   13 strict 'vars';
  2         5  
  2         1576  
19 19     36   138 local $SIG{__WARN__} = sub {};
20             eval shift
21 19         692 }
22              
23             my %sigils = qw( SCALAR $ ARRAY @ HASH % );
24              
25             sub delete_sub {
26 16     16 1 409056 my $sub = shift;
27 16 100       79 my($stashname, $key) = $sub =~ /(.*::)((?:(?!::).)*)\z/s
28             ? ($1,$2) : (caller()."::", $sub);
29 16 50       61 exists +(my $stash = \%$stashname)->{$key} or return;
30             ref $stash->{$key} eq 'SCALAR' and # perl5.10 constant
31 16 50       37 delete $stash->{$key}, return;
32 16         34 my $globname = "$stashname$key";
33 16         54 my $glob = *$globname; # autovivify the glob in case future perl
34 16 50       45 defined *$glob{CODE} or return; # versions add new funny stuff
35 16   33     150 my $check_importedness
36             = $stashname =~ /^(?:(?!\d)\w*(?:::\w*)*)\z/
37             && $key =~ /^(?!\d)\w+\z/;
38 16         22 my %imported_slots;
39             my $package;
40 16 50       28 if($check_importedness) {
41 16         27 $package = substr $stashname, 0, -2;
42 16         26 for (qw "SCALAR ARRAY HASH") {
43 48 100       552 defined *$glob{$_} or next;
44 19         57 $imported_slots{$_} = strict_eval
45             "package $package; 0 && $sigils{$_}$key; 1"
46             }
47             }
48 16         43 delete $stash->{$key};
49             keys %imported_slots == 1 and exists $imported_slots{SCALAR}
50             and !$imported_slots{SCALAR} and Internals::SvREFCNT $$glob =>== 1
51             and !defined *$glob{IO} and !defined *$glob{FORMAT}
52 16 50 66     333 and return; # empty glob
      66        
      100        
      66        
      66        
53 5         12 my $newglob = \*$globname;
54 5         12 local *alias = *$newglob;
55             defined *$glob{$_} and (
56             !$check_importedness || $imported_slots{$_}
57             ? *$newglob
58             : *alias
59             ) = *$glob{$_}
60 5 100 66     47 for qw "SCALAR ARRAY HASH";
      100        
61             defined *$glob{$_} and *$newglob = *$glob{$_}
62 5   50     15 for qw "IO FORMAT";
63             return # nothing;
64 5         21 }
65              
66             1;
67              
68             __END__