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