| 1 | |||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use v5.12.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use locale; # localize $! | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | package filename; | ||||||||||||||||||||||||||||
| 10 | # ABSTRACT: Perl module to load files at compile-time, without BEGIN blocks. | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Array::RefElem (); #qw( hv_store ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Carp 1.50 (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Scalar::Util (); #qw( blessed refaddr ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | our $VERSION = 'v0.20.060'; # VERSION | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | my ( $do, $eval, $check_inc, $error ) = (); # Private subs | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Modified version of the code as specified in `perldoc -f require` | ||||||||||||||||||||||||||||
| 26 | 2 | *import = \&require; | |||||||||||||||||||||||||||
| 27 | 248 | ▶ | sub require { | ||||||||||||||||||||||||||
|
Subroutine
require: called CC=17 SCAR=33.9
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 248 | ▶ | eval { $_[0]->isa(__PACKAGE__) } && shift | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
eval {
do {
$_[0]->isa(__PACKAGE__)
}
} and shift()
MC/DC0%0/2
eval {
do {
$_[0]->isa(__PACKAGE__)
}
} and shift()
eval {
do {
$_[0]->isa(__PACKAGE__)
}
}
shift()
Truth table33%1/3
A and B
A= eval {
do {
$_[0]->isa(__PACKAGE__)
}
}B= shift()
| |||||||||||||||||||||||||||||
| 29 | || Carp::croak( $_[0], " is not a ", __PACKAGE__ ); | ||||||||||||||||||||||||||||
| 30 | 248 | ▶ | my $filename = @_ ? shift : $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 248 | ▶ | Carp::croak("Null filename used") unless length($filename); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 248 | ▶ | if ( exists $INC{$filename} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 18 | ▶ | return 1 if $INC{$filename}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 14 | Carp::croak( | |||||||||||||||||||||||||||
| 36 | "Attempt to reload $filename aborted.\n" | ||||||||||||||||||||||||||||
| 37 | . "Compilation failed in require" | ||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 230 | ▶ | if ( File::Spec->file_name_is_absolute($filename) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 19 | ▶ | goto NOT_INC if $^V < v5.17.0 && !-r $filename; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$^V < v5.17.0 and not -r $filename
MC/DC0%0/2
$^V < v5.17.0 and not -r $filename
$^V < v5.17.0
not -r $filename
Truth table33%1/3
A and not(B)
A= $^V < v5.17.0B= not -r $filename
| |||||||||||||||||||||||||||||
| 43 | 19 | return $do->($filename); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 211 | foreach my $inc (@INC) { | |||||||||||||||||||||||||||
| 46 | 211 | my $fullpath = $check_inc->( $inc, $filename ); | |||||||||||||||||||||||||||
| 47 | 207 | ▶ | next unless length($fullpath); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 199 | ▶ | return ref($fullpath) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | ? $eval->( $$fullpath => $filename, $inc ) | ||||||||||||||||||||||||||||
| 50 | : $do->( $fullpath => $filename ); | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | NOT_INC: | ||||||||||||||||||||||||||||
| 53 | 8 | my $module = ""; | |||||||||||||||||||||||||||
| 54 | 8 | ▶ | if ( v5.18.0 <= $^V && $^V < v5.26.0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
v5.18.0 <= $^V and $^V < v5.26.0
MC/DC0%0/2
v5.18.0 <= $^V and $^V < v5.26.0
v5.18.0 <= $^V
$^V < v5.26.0
Truth table33%1/3
A and B
A= v5.18.0 <= $^VB= $^V < v5.26.0
| |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ( ( my $pm = $filename ) =~ s/\.pm\z// ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | $pm =~ s!/!::!g; | |||||||||||||||||||||||||||
| 57 | 0 | $module = $pm; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | $module = "(you may need to install the $module module) " | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | if $module; | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | Carp::croak( | ||||||||||||||||||||||||||||
| 63 | 8 | "Can't locate $filename in \@INC $module(\@INC contains: @INC)"); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | ### Private subs ### | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 211 | ▶ | $check_inc = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=19 SCAR=29.5
| |||||||||||||||||||||||||||||
| 69 | 211 | my ( $inc, $filename ) = @_; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 211 | ▶ | if ( my $ref = ref($inc) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 192 | my $subref = undef; | |||||||||||||||||||||||||||
| 73 | 192 | ▶ | if ( defined( my $pkg = Scalar::Util::blessed($inc) ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 19 | ▶ | $subref = $inc->can("INC") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | or Carp::croak( | ||||||||||||||||||||||||||||
| 76 | qq!Can't locate object method "INC" via package "$pkg"! | ||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||
| 78 | } else { | ||||||||||||||||||||||||||||
| 79 | 173 | ▶ | $subref | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 80 | = $ref eq "ARRAY" ? $inc->[0] | ||||||||||||||||||||||||||||
| 81 | : $ref eq "CODE" ? $inc | ||||||||||||||||||||||||||||
| 82 | : return; | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 188 | my @elems = $subref->( $inc, $filename ); | |||||||||||||||||||||||||||
| 85 | 188 | ▶ | return unless @elems && ( my $elem_ref = ref( $elems[0] ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@elems and my $elem_ref = ref $elems[0]
MC/DC100%2/2
@elems and my $elem_ref = ref $elems[0]
@elems
my $elem_ref = ref $elems[0]
Truth table100%3/3
A and B
A= @elemsB= my $elem_ref = ref $elems[0]
| |||||||||||||||||||||||||||||
| 86 | # Possible elements of @elems: | ||||||||||||||||||||||||||||
| 87 | # SCALAR - Prepended code | ||||||||||||||||||||||||||||
| 88 | # IO - Filehandle to read | ||||||||||||||||||||||||||||
| 89 | # CODE - Code to execute with IO | ||||||||||||||||||||||||||||
| 90 | # REF - State for CODE | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 180 | my $code = ""; | |||||||||||||||||||||||||||
| 93 | 180 | ▶ | if ( $elem_ref eq "SCALAR" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 105 | $code = ${ shift @elems }; | |||||||||||||||||||||||||||
| 95 | 105 | $elem_ref = ref( $elems[0] ); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 180 | my $fh = undef; | |||||||||||||||||||||||||||
| 99 | 180 | ▶ | if ( $elem_ref eq "GLOB" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 75 | $fh = shift @elems; | |||||||||||||||||||||||||||
| 101 | 75 | $fh = *{$fh}{IO}; | |||||||||||||||||||||||||||
| 102 | 75 | $elem_ref = ref( $elems[0] ); | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 180 | my $sub = undef; | |||||||||||||||||||||||||||
| 106 | 180 | my $state = undef; | |||||||||||||||||||||||||||
| 107 | 180 | ▶ | if ( $elem_ref eq "CODE" ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 108 | 120 | ( $sub, $state ) = @elems; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 120 | local $_; | |||||||||||||||||||||||||||
| 111 | $code .= $_ | ||||||||||||||||||||||||||||
| 112 | 120 | ▶ | while do { $_ = <$fh> if $fh; $sub->( 0, $state ); }; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 113 | } elsif ($fh) { | ||||||||||||||||||||||||||||
| 114 | 30 | local $_; | |||||||||||||||||||||||||||
| 115 | 30 | ▶ | $code .= $_ while (<$fh>); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 180 | return \$code; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 19 | ▶ | return unless length($inc); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 19 | my $fullpath = "$inc/$filename"; | |||||||||||||||||||||||||||
| 123 | 19 | ▶ | return unless -f $fullpath; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 19 | ▶ | return if $^V < v5.17.0 && !-r _; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$^V < v5.17.0 and not -r _
MC/DC0%0/2
$^V < v5.17.0 and not -r _
$^V < v5.17.0
not -r _
Truth table33%1/3
A and not(B)
A= $^V < v5.17.0B= not -r _
| |||||||||||||||||||||||||||||
| 125 | 19 | return $fullpath; | |||||||||||||||||||||||||||
| 126 | 2 | }; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 2 | my $do_text = <<'END'; | |||||||||||||||||||||||||||
| 129 | package $pkg; | ||||||||||||||||||||||||||||
| 130 | my $result = do($fullpath); | ||||||||||||||||||||||||||||
| 131 | $INC{$filename} = delete $INC{$fullpath} | ||||||||||||||||||||||||||||
| 132 | if $filename ne $fullpath && exists $INC{$fullpath}; | ||||||||||||||||||||||||||||
| 133 | if ($@) { | ||||||||||||||||||||||||||||
| 134 | $INC{$filename} && Array::RefElem::hv_store( %INC, $filename, undef ); | ||||||||||||||||||||||||||||
| 135 | die $@; | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | $result; | ||||||||||||||||||||||||||||
| 138 | END | ||||||||||||||||||||||||||||
| 139 | 38 | ▶ | $do = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 140 | 38 | ▶ | my $fullpath = @_ ? shift : $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 38 | ▶ | my $filename = @_ ? shift : $fullpath; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 38 | my ($pkg) = caller(2); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 38 | ( my $do_eval = $do_text ) =~ s/\$pkg/$pkg/; | |||||||||||||||||||||||||||
| 145 | 38 | ▶ | return eval $do_eval || $error->( $filename => $fullpath ); | ||||||||||||||||||||||||||
|
Condition100%3/3
eval $do_eval || &$error($filename, $fullpath)
MC/DC0%0/2
eval $do_eval || &$error($filename, $fullpath)
eval $do_eval
&$error($filename, $fullpath)
Truth table33%1/3
A || B
A= eval $do_evalB= &$error($filename, $fullpath)
| |||||||||||||||||||||||||||||
| 146 | 2 | }; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 2 | my $eval_text = <<'END'; | |||||||||||||||||||||||||||
| 149 | package $pkg; | ||||||||||||||||||||||||||||
| 150 | my $result = eval $code; | ||||||||||||||||||||||||||||
| 151 | Array::RefElem::hv_store( %INC, $filename, $@ ? undef : $inc ); | ||||||||||||||||||||||||||||
| 152 | die $@ if $@; | ||||||||||||||||||||||||||||
| 153 | $result; | ||||||||||||||||||||||||||||
| 154 | END | ||||||||||||||||||||||||||||
| 155 | 180 | ▶ | $eval = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 156 | 180 | ▶ | my $code = @_ ? shift : $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 180 | ▶ | my $filename = @_ ? shift : $code; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 180 | ▶ | my $inc = @_ ? shift : $filename; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 180 | my ($pkg) = caller(2); | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 180 | my $tmpfile = sprintf( "/loader/0x%x/%s", | |||||||||||||||||||||||||||
| 162 | Scalar::Util::refaddr( \$code ), | ||||||||||||||||||||||||||||
| 163 | $filename | ||||||||||||||||||||||||||||
| 164 | ); | ||||||||||||||||||||||||||||
| 165 | 180 | $code = "#line 0 $tmpfile\n" . $code; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 180 | ( my $eval_eval = $eval_text ) =~ s/\$pkg/$pkg/; | |||||||||||||||||||||||||||
| 168 | 180 | ▶ | return eval $eval_eval || $error->( $filename => $inc ); | ||||||||||||||||||||||||||
|
Condition100%3/3
eval $eval_eval || &$error($filename, $inc)
MC/DC0%0/2
eval $eval_eval || &$error($filename, $inc)
eval $eval_eval
&$error($filename, $inc)
Truth table33%1/3
A || B
A= eval $eval_evalB= &$error($filename, $inc)
| |||||||||||||||||||||||||||||
| 169 | 2 | }; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 190 | ▶ | $error = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.8
| |||||||||||||||||||||||||||||
| 172 | 190 | ▶ | my $filename = @_ ? shift : $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 190 | ▶ | my $fullpath = @_ ? shift : $filename; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 190 | ▶ | $@ && Carp::croak( $@, "Compilation failed in require" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | # $INC{$filename} shouldn't be set if we've gotten here, | ||||||||||||||||||||||||||||
| 178 | # and $! is invalid if $INC{$filename} is true. | ||||||||||||||||||||||||||||
| 179 | 176 | ▶ | delete $INC{$filename} && undef($!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 176 | ▶ | $! && Carp::croak( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 182 | "Can't locate $filename: ", | ||||||||||||||||||||||||||||
| 183 | $^V >= v5.21.0 ? "$fullpath: " : (), | ||||||||||||||||||||||||||||
| 184 | "$!" | ||||||||||||||||||||||||||||
| 185 | ); | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 168 | Carp::croak( $filename, " did not return a true value" ); | |||||||||||||||||||||||||||
| 188 | 2 | }; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 2 | 1; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | __END__ | ||||||||||||||||||||||||||||