Safe/Safe.pm

filter: Statementstmt 92.3% 48 / 52 Branchbran 81.2% 13 / 16 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 87.3% 76 / 87 CC 10 SCAR 14.7
CC
10
Cov
87%
eval
8.6
new
3.1
BEGIN
1.0
CRAP 4.3
« Safe.pm ↑ Summary Safe/Eval.pm »
1 # This is an implementation of Eval::Safe that uses the Safe module to execute
2 # the user provided code.
3  
4 package Eval::Safe::Safe;
5  
6 13 ▶use 5.022;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 ▶use parent 'Eval::Safe';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Eval::Safe::ForkedSafe;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use File::Spec::Functions qw(rel2abs);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 48 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod documented
17 48 my ($class, %options) = @_;
18 48 my $self = bless \%options, $class;
19 48 my $safe = Eval::Safe::ForkedSafe->new($self->{package});
20 48 ▶ $self->{package} = $safe->root() unless $self->{package};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"package"} 43 5 48
21 # This option is always set if we're building an Eval::Safe::Safe.
22 48 ▶ if ($self->{safe} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'safe'} > 1) { } 0 48 48
23 0 $safe->permit_only(qw(:base_core :base_mem :base_loop :base_math :base_orig
24 :load));
25 0 $safe->deny(qw(tie untie bless));
26 } else {
27 48 $safe->deny_only(qw(:subprocess :ownprocess :others :dangerous));
28 }
29 48 $self->{safe} = $safe;
30 48 return $self;
31 }
32  
33  
34 48 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
35 48 local($., $@, $!, $^E, $?);
36 48 my ($this) = @_;
37 48 delete $this->{safe};
38 # The package is not entirely deleted by the Safe destructor.
39 48 CORE::eval('undef %'.($this->{package}).'::');
40 }
41  
42  
43 43 ▶sub eval {
Subroutine eval: called CC=8 SCAR=21.5
Pod documented
44 43 my ($this, $code) = @_;
45 43 my $eval_str = sprintf "%s; %s; %s", $this->{strict}, $this->{warnings}, $code;
46 0 ▶ print {$this->{debug}} "Evaling (safe): '${eval_str}'\n" if $this->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"debug"} 0 43 43
47 43 my @ret;
48 43 ▶ if (not defined wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined wantarray) { } 14 29 43
elsif (wantarray) { } 4 25 29
49 14 $this->{safe}->reval($eval_str);
50 } elsif (wantarray) {
51 4 @ret = $this->{safe}->reval($eval_str);
52 } else {
53 25 @ret = scalar $this->{safe}->reval($eval_str);
54 }
55 0 ▶ print {$this->{debug}} "Safe returned an error: $@" if $this->{debug} && $@;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"debug"} and $@ 0 43 43
Condition33%1/3
$this->{"debug"} and $@
!ll&&!rl&&r
4300
MC/DC0%0/2
$this->{"debug"} and $@
$this->{"debug"} $@
Truth table33%1/3
A and B
A= $this->{"debug"}B= $@
ABresult
0X0
100
111
56 43 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 29 14 43
57 29 ▶ return (wantarray) ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 25 29
58 }
59 14 return;
60 }
61  
62 3 ▶sub do {
Subroutine do: called CC=1 SCAR=0.0
Pod documented
63 3 my ($this, $file) = @_;
64 # do can open relative paths, but in that case it looks them up in the @INC
65 # directory, which we want to avoid.
66 # We don't use abs_path here to not die (just yet) if the file does not exist.
67 3 my $abs_path = rel2abs($file);
68 3 $this->{safe}->rdo($abs_path);
69 }
70  
71 13 1;
« Safe.pm ↑ Summary Safe/Eval.pm »