File Coverage

blib/lib/Sub/Delete.pm
Criterion Covered Total %
statement 38 38 100.0
branch 11 16 68.7
condition 18 25 72.0
subroutine 7 7 100.0
pod 0 2 0.0
total 74 88 84.0


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