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 0 2 0.0
total 71 86 82.5


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