| 1 | package SelfLoader; | ||
| 2 | use 5.008; | ||
| 3 | use strict; | ||
| 4 | use IO::Handle; | ||
| 5 | our $VERSION = "1.23"; | ||
| 6 | |||
| 7 | # The following bit of eval-magic is necessary to make this work on | ||
| 8 | # perls < 5.009005. | ||
| 9 | use vars qw/$AttrList/; | ||
| 10 | BEGIN { | ||
| 11 | if ($] > 5.009004) { | ||
| 12 | eval <<'NEWERPERL'; | ||
| 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 | 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 | use Exporter; | ||
| 43 | our @ISA = qw(Exporter); | ||
| 44 | our @EXPORT = qw(AUTOLOAD); | ||
| 45 | sub Version {$VERSION} | ||
| 46 | sub DEBUG () { 0 } | ||
| 47 | |||
| 48 | my %Cache; # private cache for all SelfLoader's client packages | ||
| 49 | |||
| 50 | # in croak and carp, protect $@ from "require Carp;" RT #40216 | ||
| 51 | |||
| 52 | sub croak { { local $@; require Carp; } goto &Carp::croak } | ||
| 53 | sub carp { { local $@; require Carp; } goto &Carp::carp } | ||
| 54 | |||
| 55 | AUTOLOAD { | ||
| 56 | our $AUTOLOAD; | ||
| 57 | print STDERR "SelfLoader::AUTOLOAD for $AUTOLOAD\n" if DEBUG; | ||
| 58 | my $SL_code = $Cache{$AUTOLOAD}; | ||
| 59 | my $save = $@; # evals in both AUTOLOAD and _load_stubs can corrupt $@ | ||
| 60 | unless ($SL_code) { | ||
| 61 | # Maybe this pack had stubs before __DATA__, and never initialized. | ||
| 62 | # Or, this maybe an automatic DESTROY method call when none exists. | ||
| 63 | $AUTOLOAD =~ m/^(.*)::/; | ||
| 64 | SelfLoader->_load_stubs($1) unless exists $Cache{"${1}::<DATA"}; | ||
| 65 | $SL_code = $Cache{$AUTOLOAD}; | ||
| 66 | $SL_code = "sub $AUTOLOAD { }" | ||
| 67 | if (!$SL_code and $AUTOLOAD =~ m/::DESTROY$/); | ||
| 68 | croak "Undefined subroutine $AUTOLOAD" unless $SL_code; | ||
| 69 | } | ||
| 70 | print STDERR "SelfLoader::AUTOLOAD eval: $SL_code\n" if DEBUG; | ||
| 71 | |||
| 72 | { | ||
| 73 | no strict; | ||
| 74 | eval $SL_code; | ||
| 75 | } | ||
| 76 | if ($@) { | ||
| 77 | $@ =~ s/ at .*\n//; | ||
| 78 | croak $@; | ||
| 79 | } | ||
| 80 | $@ = $save; | ||
| 81 | defined(&$AUTOLOAD) || die "SelfLoader inconsistency error"; | ||
| 82 | delete $Cache{$AUTOLOAD}; | ||
| 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, $protoype); | ||
| 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 | ||
| 104 | # paranoid | ||
| 105 | open $fh, '<&', $nfh or croak "reopen2: $!"; # dup() the fd "back" | ||
| 106 | close $nfh or die "close after reopen: $!"; # autocloses, but be | ||
| 107 | # paranoid | ||
| 108 | $fh->untaint; | ||
| 109 | } | ||
| 110 | $Cache{"${currpack}::<DATA"} = 1; # indicate package is cached | ||
| 111 | |||
| 112 | local($/) = "\n"; | ||
| 113 | while(defined($line = <$fh>) and $line !~ m/^__END__/) { | ||
| 114 | if ($line =~ m/ ^\s* # indentation | ||
| 115 | sub\s+([\w:]+)\s* # 'sub' and sub name | ||
| 116 | ( | ||
| 117 | (?:\([\\\$\@\%\&\*\;]*\))? # optional prototype sigils | ||
| 118 | (?:$AttrList)? # optional attribute list | ||
| 119 | )/x) { | ||
| 120 | push(@stubs, $self->_add_to_cache($name, $currpack, | ||
| 121 | \@lines, $protoype)); | ||
| 122 | $protoype = $2; | ||
| 123 | @lines = ($line); | ||
| 124 | if (index($1,'::') == -1) { # simple sub name | ||
| 125 | $name = "${currpack}::$1"; | ||
| 126 | } else { # sub name with package | ||
| 127 | $name = $1; | ||
| 128 | $name =~ m/^(.*)::/; | ||
| 129 | if (defined(&{"${1}::AUTOLOAD"})) { | ||
| 130 | \&{"${1}::AUTOLOAD"} == \&SelfLoader::AUTOLOAD || | ||
| 131 | die 'SelfLoader Error: attempt to specify Selfloading', | ||
| 132 | " sub $name in non-selfloading module $1"; | ||
| 133 | } else { | ||
| 134 | $self->export($1,'AUTOLOAD'); | ||
| 135 | } | ||
| 136 | } | ||
| 137 | } elsif ($line =~ m/^package\s+([\w:]+)/) { # A package declared | ||
| 138 | push(@stubs, $self->_add_to_cache($name, $currpack, | ||
| 139 | \@lines, $protoype)); | ||
| 140 | $self->_package_defined($line); | ||
| 141 | $name = ''; | ||
| 142 | @lines = (); | ||
| 143 | $currpack = $1; | ||
| 144 | $Cache{"${currpack}::<DATA"} = 1; # indicate package is cached | ||
| 145 | if (defined(&{"${1}::AUTOLOAD"})) { | ||
| 146 | \&{"${1}::AUTOLOAD"} == \&SelfLoader::AUTOLOAD || | ||
| 147 | die 'SelfLoader Error: attempt to specify Selfloading', | ||
| 148 | " package $currpack which already has AUTOLOAD"; | ||
| 149 | } else { | ||
| 150 | $self->export($currpack,'AUTOLOAD'); | ||
| 151 | } | ||
| 152 | } else { | ||
| 153 | push(@lines,$line); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | if (defined($line) && $line =~ /^__END__/) { # __END__ | ||
| 157 | unless ($line =~ /^__END__\s*DATA/) { | ||
| 158 | if ($endlines) { | ||
| 159 | # Devel::SelfStubber would like us to capture the lines after | ||
| 160 | # __END__ so it can write out the entire file | ||
| 161 | @$endlines = <$fh>; | ||
| 162 | } | ||
| 163 | close($fh); | ||
| 164 | } | ||
| 165 | } | ||
| 166 | push(@stubs, $self->_add_to_cache($name, $currpack, \@lines, $protoype)); | ||
| 167 | no strict; | ||
| 168 | eval join('', @stubs) if @stubs; | ||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | sub _add_to_cache { | ||
| 173 | my($self,$fullname,$pack,$lines, $protoype) = @_; | ||
| 174 | return () unless $fullname; | ||
| 175 | carp("Redefining sub $fullname") | ||
| 176 | if exists $Cache{$fullname}; | ||
| 177 | $Cache{$fullname} = join('', | ||
| 178 | "\n\#line 1 \"sub $fullname\"\npackage $pack; ", | ||
| 179 | @$lines); | ||
| 180 | #$Cache{$fullname} = join('', "package $pack; ",@$lines); | ||
| 181 | print STDERR "SelfLoader cached $fullname: $Cache{$fullname}" if DEBUG; | ||
| 182 | # return stub to be eval'd | ||
| 183 | defined($protoype) ? "sub $fullname $protoype;" : "sub $fullname;" | ||
| 184 | } | ||
| 185 | |||
| 186 | sub _package_defined {} | ||
| 187 | |||
| 188 | 1; | ||
| 189 | __END__ |