blib/lib/Devel/Trepan/DB/SelfLoader.pm untested

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 239 CC 25 SCAR 42.4
CC
25
Cov
0%
_load_stubs
182.0
AUTOLOAD
72.0
_add_to_cache
30.0
CRAP 69.7
1 package # hide me from PAUSE
2 SelfLoader;
3 use 5.008;
4 use strict;
5 use IO::Handle;
6 our $VERSION = "1.20";
7  
8 # The following bit of eval-magic is necessary to make this work on
9 # perls < 5.009005.
10 use vars qw/$AttrList/;
11 BEGIN {
12 if ($] > 5.009004) {
13 eval <<'NEWERPERL';
14 use 5.009005; # due to new regexp features
15 # allow checking for valid ': attrlist' attachments
16 # see also AutoSplit
17 $AttrList = qr{
18 \s* : \s*
19 (?:
20 # one attribute
21 (?> # no backtrack
22 (?! \d) \w+
23 (?<nested> \( (?: [^()]++ | (?&nested)++ )*+ \) ) ?
24 )
25 (?: \s* : \s* | \s+ (?! :) )
26 )*
27 }x;
28  
29 NEWERPERL
30 }
31 else {
32 eval <<'OLDERPERL';
33 # allow checking for valid ': attrlist' attachments
34 # (we use 'our' rather than 'my' here, due to the rather complex and buggy
35 # behaviour of lexicals with qr// and (??{$lex}) )
36 our $nested;
37 $nested = qr{ \( (?: (?> [^()]+ ) | (??{ $nested }) )* \) }x;
38 our $one_attr = qr{ (?> (?! \d) \w+ (?:$nested)? ) (?:\s*\:\s*|\s+(?!\:)) }x;
39 $AttrList = qr{ \s* : \s* (?: $one_attr )* }x;
40 OLDERPERL
41 }
42 }
43 use Exporter;
44 our @ISA = qw(Exporter);
45 our @EXPORT = qw(AUTOLOAD);
46 sub Version {$VERSION}
47 sub DEBUG () { 0 }
48  
49 our %Cache; # private cache for all SelfLoader's client packages
50  
51 # in croak and carp, protect $@ from "require Carp;" RT #40216
52  
53 sub croak { { local $@; require Carp; } goto &Carp::croak }
54 sub carp { { local $@; require Carp; } goto &Carp::carp }
55  
56 AUTOLOAD {
57 our $AUTOLOAD;
58 print STDERR "SelfLoader::AUTOLOAD for $AUTOLOAD\n" if DEBUG;
59 my $SL_code = $Cache{$AUTOLOAD};
60 my $save = $@; # evals in both AUTOLOAD and _load_stubs can corrupt $@
61 unless ($SL_code) {
62 # Maybe this pack had stubs before __DATA__, and never initialized.
63 # Or, this maybe an automatic DESTROY method call when none exists.
64 $AUTOLOAD =~ m/^(.*)::/;
65 SelfLoader->_load_stubs($1) unless exists $Cache{"${1}::<DATA"};
66 $SL_code = $Cache{$AUTOLOAD};
67 $SL_code = "sub $AUTOLOAD { }"
68 if (!$SL_code and $AUTOLOAD =~ m/::DESTROY$/);
69 croak "Undefined subroutine $AUTOLOAD" unless $SL_code;
70 }
71 print STDERR "SelfLoader::AUTOLOAD eval: $SL_code\n" if DEBUG;
72  
73 {
74 no strict;
75 eval $SL_code;
76 }
77 if ($@) {
78 $@ =~ s/ at .*\n//;
79 croak $@;
80 }
81 $@ = $save;
82 defined(&$AUTOLOAD) || die "SelfLoader inconsistency error";
83 goto &$AUTOLOAD
84 }
85  
86 sub load_stubs { shift->_load_stubs((caller)[0]) }
87  
88 sub _load_stubs {
89 # $endlines is used by Devel::SelfStubber to capture lines after __END__
90 my($self, $callpack, $endlines) = @_;
91 no strict "refs";
92 my $fh = \*{"${callpack}::DATA"};
93 use strict;
94 my $currpack = $callpack;
95 my($line, $name, @lines, @stubs, $prototype);
96  
97 print STDERR "SelfLoader::load_stubs($callpack)\n" if DEBUG;
98 croak("$callpack doesn't contain an __DATA__ token")
99 unless defined fileno($fh);
100 # Protect: fork() shares the file pointer between the parent and the kid
101 if(sysseek($fh, tell($fh), 0)) {
102 open my $nfh, '<&', $fh or croak "reopen: $!";# dup() the fd
103 close $fh or die "close: $!"; # autocloses, but be paranoid
104 open $fh, '<&', $nfh or croak "reopen2: $!"; # dup() the fd "back"
105 close $nfh or die "close after reopen: $!"; # autocloses, but be paranoid
106 $fh->untaint;
107 }
108 $Cache{"${currpack}::<DATA"} = 1; # indicate package is cached
109  
110 local($/) = "\n";
111 while(defined($line = <$fh>) and $line !~ m/^__END__/) {
112 if ($line =~ m/^\s*sub\s+([\w:]+)\s*((?:\([\\\$\@\%\&\*\;]*\))?(?:$AttrList)?)/) {
113 push(@stubs,
114 $self->_add_to_cache($name, $currpack, \@lines, $prototype));
115 $prototype = $2;
116 @lines = ($line);
117 if (index($1,'::') == -1) { # simple sub name
118 $name = "${currpack}::$1";
119 } else { # sub name with package
120 $name = $1;
121 $name =~ m/^(.*)::/;
122 if (defined(&{"${1}::AUTOLOAD"})) {
123 \&{"${1}::AUTOLOAD"} == \&SelfLoader::AUTOLOAD ||
124 die 'SelfLoader Error: attempt to specify Selfloading',
125 " sub $name in non-selfloading module $1";
126 } else {
127 $self->export($1,'AUTOLOAD');
128 }
129 }
130 } elsif ($line =~ m/^package\s+([\w:]+)/) { # A package declared
131 push(@stubs,
132 $self->_add_to_cache($name, $currpack, \@lines, $prototype));
133 $self->_package_defined($line);
134 $name = '';
135 @lines = ();
136 $currpack = $1;
137 $Cache{"${currpack}::<DATA"} = 1; # indicate package is cached
138 if (defined(&{"${1}::AUTOLOAD"})) {
139 \&{"${1}::AUTOLOAD"} == \&SelfLoader::AUTOLOAD ||
140 die 'SelfLoader Error: attempt to specify Selfloading',
141 " package $currpack which already has AUTOLOAD";
142 } else {
143 $self->export($currpack,'AUTOLOAD');
144 }
145 } else {
146 push(@lines,$line);
147 }
148 }
149 if (defined($line) && $line =~ /^__END__/) { # __END__
150 unless ($line =~ /^__END__\s*DATA/) {
151 if ($endlines) {
152 # Devel::SelfStubber would like us to capture the lines after
153 # __END__ so it can write out the entire file
154 @$endlines = <$fh>;
155 }
156 close($fh);
157 }
158 }
159 push(@stubs,
160 $self->_add_to_cache($name, $currpack, \@lines, $prototype));
161 no strict;
162 eval join('', @stubs) if @stubs;
163 }
164  
165  
166 sub _add_to_cache {
167 my($self, $funcname, $pack, $lines, $prototype) = @_;
168 return () unless $funcname;
169 carp("Redefining sub $funcname") if exists $Cache{$funcname};
170 my $header = qq(\n\#line 1 "sub $funcname"\npackage $pack; );
171 $Cache{$funcname} = join('', $header, @$lines);
172 print STDERR "SelfLoader cached $funcname: $Cache{$funcname}" if DEBUG;
173 # return stub to be eval'd
174 defined($prototype) ? "sub $funcname $prototype;" : "sub $funcname;"
175 }
176  
177 sub _package_defined {}
178  
179 1;
180  
181  
182  
183 __END__