blib/lib/CGI/Compile.pm

filter: Statementstmt 98.2% 115 / 117 Branchbran 77.2% 34 / 44 Conditioncond 52.0% 13 / 25 MC/DCmcdc 12.5% 2 / 16 Subroutinesub 91.6% 22 / 24 Podpod 100% 2 / 2 totaltotal 82.4% 188 / 228 CC 34 SCAR 16.5
CC
34
Cov
87%
compile
12.9
_eval
6.2
_build_subname
5.1
CRAP 5.2
↑ Summary Build »
1 package CGI::Compile;
2  
3 3216 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3216 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3216 our $VERSION = '0.26';
7  
8 3216 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3216 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3216 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3216 ▶use File::pushd;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3216 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3216 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3216 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3216 ▶use Sub::Name 'subname';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3216 our $RETURN_EXIT_VAL = undef;
18  
19 sub new {
20 3260 ▶ my ($class, %opts) = @_;
Subroutine new: called CC=2 SCAR=7.1
Pod documented
21  
22 3260 ▶ $opts{namespace_root} ||= 'CGI::Compile::ROOT';
Condition50%1/2
$opts{"namespace_root"} ||= "CGI::Compile::ROOT"
l!l
03260
MC/DC0%0/1
$opts{"namespace_root"} ||= "CGI::Compile::ROOT"
$opts{"namespace_root"}
Truth table50%1/2
A
A= $opts{"namespace_root"}
Aresult
11
00
23  
24 3260 bless \%opts, $class;
25 }
26  
27 3216 our $USE_REAL_EXIT;
28 BEGIN {
29 3216 ▶ $USE_REAL_EXIT = 1;
Subroutine BEGIN: called CC=5 SCAR=16.1
30  
31 3216 my $orig = *CORE::GLOBAL::exit{CODE};
32  
33 3216 ▶ my $proto = $orig ? prototype $orig : prototype 'CORE::exit';
Branch: 1 branch
BranchTrueFalseTotal
$orig ? : 1 3215 3216
34  
35 3216 ▶ $proto = $proto ? "($proto)" : '';
Branch: 1 branch
BranchTrueFalseTotal
$proto ? : 3216 0 3216
36  
37 $orig ||= sub {
38 3201 ▶ my $exit_code = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
39  
40 3201 ▶ CORE::exit(defined $exit_code ? $exit_code : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $exit_code ? : 3200 1 3201
41 3216 ▶ };
Condition66%2/3
$orig ||= sub { my $exit_code = shift(); exit(defined $exit_code ? $exit_code : 0); }
l!l&&r!l&&!r
132150
MC/DC0%0/2
$orig ||= sub { my $exit_code = shift(); exit(defined $exit_code ? $exit_code : 0); }
$orig sub { my $exit_code = shift(); exit(defined $exit_code ? $exit_code : 0); }
Truth table66%2/3
A ||= B
A= $origB= sub { my $exit_code = shift(); exit(defined $exit_code ? $exit_code : 0); }
ABresult
1X1
011
000
42  
43 3216 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 3216 ▶ *CORE::GLOBAL::exit = eval qq{
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::Compile::USE_REAL_EXIT 3202 3222 6424
Condition100%2/2
$exit_code || 0
l!l
73216
MC/DC100%1/1
$exit_code || 0
$exit_code
Truth table100%2/2
A
A= $exit_code
Aresult
11
00
46 sub $proto {
47 my \$exit_code = shift;
48  
49 \$orig->(\$exit_code) if \$USE_REAL_EXIT;
50  
51 die [ "EXIT\n", \$exit_code || 0 ]
52 };
53 };
54 3216 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3216 3216
55 }
56  
57 3216 my %anon;
58  
59 sub compile {
60 3260 ▶ my($class, $script, $package) = @_;
Subroutine compile: called CC=12 SCAR=25.5
Pod documented
61  
62 3260 ▶ my $self = ref $class ? $class : $class->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 13 3247 3260
63  
64 3260 my($code, $path, $dir, $subname);
65  
66 3260 ▶ if (ref $script eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $script eq 'SCALAR') { } 50 3210 3260
67 50 $code = $$script;
68  
69 50 ▶ $package ||= (caller)[0];
Condition33%1/3
$package ||= (caller)[0]
l!l&&r!l&&!r
0500
MC/DC0%0/2
$package ||= (caller)[0]
$package (caller)[0]
Truth table33%1/3
A ||= B
A= $packageB= (caller)[0]
ABresult
1X1
011
000
70  
71 50 $subname = '__CGI' . $anon{$package}++ . '__';
72 } else {
73 3210 $code = $self->_read_source($script);
74  
75 3210 $path = Cwd::abs_path($script);
76 3210 $dir = File::Basename::dirname($path);
77  
78 3210 my $genned_package;
79  
80 3210 ▶ ($genned_package, $subname) = $self->_build_subname($path || $script);
Condition33%1/3
$path || $script
l!l&&r!l&&!r
321000
MC/DC0%0/2
$path || $script
$path $script
Truth table33%1/3
A || B
A= $pathB= $script
ABresult
1X1
011
000
81  
82 3210 ▶ $package ||= $genned_package;
Condition33%1/3
$package ||= $genned_package
l!l&&r!l&&!r
032100
MC/DC0%0/2
$package ||= $genned_package
$package $genned_package
Truth table33%1/3
A ||= B
A= $packageB= $genned_package
ABresult
1X1
011
000
83 }
84  
85 3260 ▶ my $warnings = $code =~ /^#!.*\s-w\b/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$code =~ /^#!.*\s-w\b/ ? : 1 3259 3260
86 3260 $code =~ s/^__END__\r?\n.*//ms;
87 3260 $code =~ s/^__DATA__\r?\n(.*)//ms;
88 3260 ▶ my $data = defined $1 ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 3205 55 3260
89  
90 # TODO handle nph and command line switches?
91 3260 ▶ my $eval = join '',
Branch: 3 branches
BranchTrueFalseTotal
$path ? : 3210 50 3260
$dir ? : 3210 50 3260
$path ? : 3210 50 3260
92 "package $package;",
93 'sub {',
94 'local $CGI::Compile::USE_REAL_EXIT = 0;',
95 "\nCGI::initialize_globals() if defined &CGI::initialize_globals;",
96 'local ($0, $CGI::Compile::_dir, *DATA);',
97 '{ my ($data, $path, $dir) = @_[1..3];',
98 ($path ? '$0 = $path;' : ''),
99 ($dir ? '$CGI::Compile::_dir = File::pushd::pushd $dir;' : ''),
100 q{open DATA, '<', \$data;},
101 '}',
102 # NOTE: this is a workaround to fix a problem in Perl 5.10
103 q(local @SIG{keys %SIG} = do { no warnings 'uninitialized'; @{[]} = values %SIG };),
104 "local \$^W = $warnings;",
105 'my $rv = eval {',
106 'local @ARGV = @{ $_[4] };', # args to @ARGV
107 'local @_ = @{ $_[4] };', # args to @_ as well
108 ($path ? "\n#line 1 $path\n" : ''),
109 $code,
110 "\n};",
111 q{
112 {
113 no warnings qw(uninitialized numeric pack);
114 my $self = shift;
115 my $exit_val = unpack('C', pack('C', sprintf('%.0f', $rv)));
116 if ($@) {
117 die $@ unless (
118 ref($@) eq 'ARRAY' and
119 $@->[0] eq "EXIT\n"
120 );
121 my $exit_param = unpack('C', pack('C', sprintf('%.0f', $@->[1])));
122  
123 if ($exit_param != 0 && !$CGI::Compile::RETURN_EXIT_VAL && !$self->{return_exit_val}) {
124 die "exited nonzero: $exit_param";
125 }
126  
127 $exit_val = $exit_param;
128 }
129  
130 return $exit_val;
131 }
132 },
133 '};';
134  
135 3260 my $sub = do {
136 3216 ▶ no warnings 'uninitialized'; # for 5.8
Subroutine BEGIN: called CC=1 SCAR=0.0
137 # NOTE: this is a workaround to fix a problem in Perl 5.10
138 3260 local @SIG{keys %SIG} = @{[]} = values %SIG;
139 3260 local $USE_REAL_EXIT = 0;
140  
141 3260 my $code = $self->_eval($eval);
142 3260 my $exception = $@;
143  
144 3260 ▶ die "Could not compile $script: $exception" if $exception;
Branch: 1 branch
BranchTrueFalseTotal
if $exception 6 3254 3260
145  
146 subname "${package}::$subname", sub {
147 3255 ▶ my @args = @_;
Subroutine __ANON__: called CC=2 SCAR=9.7
148 # this is necessary for MSWin32
149 3255 ▶ my $orig_warn = $SIG{__WARN__} || sub { warn(@_) };
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$SIG{'__WARN__'} || sub { warn @_; }
l!l&&r!l&&!r
032550
MC/DC0%0/2
$SIG{'__WARN__'} || sub { warn @_; }
$SIG{'__WARN__'} sub { warn @_; }
Truth table33%1/3
A || B
A= $SIG{'__WARN__'}B= sub { warn @_; }
ABresult
1X1
011
000
150 3255 ▶ local $SIG{__WARN__} = sub { $orig_warn->(@_) unless $_[0] =~ /^No such signal/ };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^No such signal/ 0 0 0
151 3255 $code->($self, $data, $path, $dir, \@args)
152 3254 };
153 };
154  
155 3254 return $sub;
156 }
157  
158 sub _read_source {
159 3210 ▶ my($self, $file) = @_;
Subroutine _read_source: called CC=2 SCAR=7.0
160  
161 3210 ▶ open my $fh, "<", $file or die "$file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "${file}: $!" 0 3210 3210
162 3210 return do { local $/; <$fh> };
163 }
164  
165 sub _build_subname {
166 3210 ▶ my($self, $path) = @_;
Subroutine _build_subname: called CC=5 SCAR=16.2
167  
168 3210 my ($volume, $dirs, $file) = File::Spec::Functions::splitpath($path);
169 3210 my @dirs = File::Spec::Functions::splitdir($dirs);
170  
171 3210 my $name = $file;
172 3210 ▶ my $package = join '_', grep { defined && length } $volume, @dirs, $name;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 32100 0 32100
173  
174 # Escape everything into valid perl identifiers
175 3210 s/([^A-Za-z0-9_])/sprintf("_%2x", unpack("C", $1))/eg for $package, $name;
176  
177 # make sure the identifiers don't start with a digit
178 3210 s/^(\d)/_$1/ for $package, $name;
179  
180 3210 ▶ $package = $self->{namespace_root} . ($package ? "::$package" : '');
Branch: 1 branch
BranchTrueFalseTotal
$package ? : 3210 0 3210
181  
182 3210 return ($package, $name);
183 }
184  
185 # define tmp_dir value later on first usage, otherwise all children
186 # share the same directory when forked
187 my $tmp_dir;
188 sub _eval {
189 3260 ▶ my $code = \$_[1];
Subroutine _eval: called CC=6 SCAR=18.3
190  
191 # we use a tmpdir chmodded to 0700 so that the tempfiles are secure
192 3260 ▶ $tmp_dir ||= File::Spec->catfile(File::Spec->tmpdir, "cgi_compile_$$");
Condition66%2/3
$tmp_dir ||= "File::Spec"->catfile("File::Spec"->tmpdir, "cgi_compile_${$}")
l!l&&r!l&&!r
4832120
MC/DC0%0/2
$tmp_dir ||= "File::Spec"->catfile("File::Spec"->tmpdir, "cgi_compile_${$}")
$tmp_dir "File::Spec"->catfile("File::Spec"->tmpdir, "cgi_compile_${$}")
Truth table66%2/3
A ||= B
A= $tmp_dirB= "File::Spec"->catfile("File::Spec"->tmpdir, "cgi_compile_${$}")
ABresult
1X1
011
000
193  
194 3260 ▶ if (! -d $tmp_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $tmp_dir) 3212 48 3260
195 3212 ▶ mkdir $tmp_dir or die "Could not mkdir $tmp_dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $tmp_dir or die "Could not mkdir ${tmp_dir}: $!" 0 3212 3212
196 3212 ▶ chmod 0700, $tmp_dir or die "Could not chmod 0700 $tmp_dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
chmod 0700, $tmp_dir or die "Could not chmod 0700 ${tmp_dir}: $!" 0 3212 3212
197 }
198  
199 3260 my ($fh, $fname) = File::Temp::tempfile('cgi_compile_XXXXX',
200 UNLINK => 1, SUFFIX => '.pm', DIR => $tmp_dir);
201  
202 3260 print $fh $$code;
203 3260 close $fh;
204  
205 3260 my $sub = do $fname;
206  
207 3260 ▶ unlink $fname or die "Could not delete $fname: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $fname or die "Could not delete ${fname}: $!" 0 3260 3260
208  
209 3260 return $sub;
210 }
211  
212 END {
213 3216 ▶ if ($tmp_dir and -d $tmp_dir) {
Subroutine END: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp_dir and -d $tmp_dir) 3212 4 3216
Condition66%2/3
$tmp_dir and -d $tmp_dir
!ll&&!rl&&r
403212
MC/DC50%1/2
$tmp_dir and -d $tmp_dir
$tmp_dir -d $tmp_dir
Truth table66%2/3
A and B
A= $tmp_dirB= -d $tmp_dir
ABresult
0X0
100
111
214 3212 File::Path::remove_tree($tmp_dir);
215 }
216 }
217  
218 3216 1;
219  
220 __END__
↑ Summary Build »