| 1 | # This is an implementation of Eval::Safe that uses `eval` to execute the user | ||||||||||||||||||||||||||||
| 2 | # provided code. | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Eval::Safe::Eval; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 19 | ▶ | use 5.022; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 19 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 19 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 14 | ▶ | use parent 'Eval::Safe'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 14 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 14 | ▶ | use File::Spec::Functions qw(rel2abs); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | # Count the number of Eval::Safe::Eval object created to assign each of them a | ||||||||||||||||||||||||||||
| 16 | # specific package name. | ||||||||||||||||||||||||||||
| 17 | 13 | my $env_count = 0; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 49 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 50 | my ($class, %options) = @_; | |||||||||||||||||||||||||||
| 21 | 49 | my $self = bless \%options, $class; | |||||||||||||||||||||||||||
| 22 | 49 | ▶ | $self->{package} = 'Eval::Safe::Eval::Env'.($env_count++) unless $self->{package}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 49 | return $self; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 49 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 49 | local($., $@, $!, $^E, $?); | |||||||||||||||||||||||||||
| 28 | 49 | my ($this) = @_; | |||||||||||||||||||||||||||
| 29 | 49 | CORE::eval('undef %'.($this->{package}).'::'); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 46 | ▶ | sub eval { | ||||||||||||||||||||||||||
|
Subroutine
eval: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 46 | my ($this, $code) = @_; | |||||||||||||||||||||||||||
| 34 | my $eval_str = sprintf "package %s; %s; %s; %s", $this->{package}, | ||||||||||||||||||||||||||||
| 35 | 46 | $this->{strict}, $this->{warnings}, $code; | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | print {$this->{debug}} "Evaling (eval): '${eval_str}'\n" if $this->{debug}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 46 | my @ret; | |||||||||||||||||||||||||||
| 38 | 46 | ▶ | if (not defined wantarray) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 39 | 14 | CORE::eval($eval_str); | |||||||||||||||||||||||||||
| 40 | } elsif (wantarray) { | ||||||||||||||||||||||||||||
| 41 | 4 | ▶ | @ret = CORE::eval($eval_str); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | } else { | ||||||||||||||||||||||||||||
| 43 | 28 | ▶ | @ret = scalar CORE::eval($eval_str); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | print {$this->{debug}} "Eval returned an error: $@" if $this->{debug} && $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$this->{"debug"} and $@
MC/DC0%0/2
$this->{"debug"} and $@
$this->{"debug"}
$@
Truth table33%1/3
A and B
A= $this->{"debug"}B= $@
| |||||||||||||||||||||||||||||
| 46 | 46 | return $this->_wrap_code_refs(\&_wrap_in_eval, @ret); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 3 | ▶ | sub do { | ||||||||||||||||||||||||||
|
Subroutine
do: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | 3 | my ($this, $file) = @_; | |||||||||||||||||||||||||||
| 51 | # do can open relative paths, but in that case it looks them up in the @INC | ||||||||||||||||||||||||||||
| 52 | # directory, which we want to avoid. | ||||||||||||||||||||||||||||
| 53 | # We don't use abs_path here to not die (just yet) if the file does not exist. | ||||||||||||||||||||||||||||
| 54 | 3 | my $abs_path = rel2abs($file); | |||||||||||||||||||||||||||
| 55 | 3 | $this->eval("my \$r = do '${abs_path}'; die \$@ if \$@; \$r"); | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # To emulate the behavior of the Safe approach (where code returned by eval is | ||||||||||||||||||||||||||||
| 59 | # wrapped to trap all exception, we're using this method to wrap code returned | ||||||||||||||||||||||||||||
| 60 | # by eval in the same way). | ||||||||||||||||||||||||||||
| 61 | 8 | ▶ | sub _wrap_in_eval { | ||||||||||||||||||||||||||
|
Subroutine
_wrap_in_eval: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 8 | my ($this, $sub) = @_; | |||||||||||||||||||||||||||
| 63 | # When $sub is called, we're executing it in an `eval` and also wrapping all | ||||||||||||||||||||||||||||
| 64 | # its returned code in the same way. | ||||||||||||||||||||||||||||
| 65 | 8 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 66 | 8 | my @ret; | |||||||||||||||||||||||||||
| 67 | 8 | ▶ | if (not defined wantarray) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 68 | 2 | eval { $sub->() }; | |||||||||||||||||||||||||||
| 69 | } elsif (wantarray) { | ||||||||||||||||||||||||||||
| 70 | 1 | @ret = eval { $sub->() }; | |||||||||||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||||||||||
| 72 | 5 | @ret = scalar eval { $sub->() }; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 8 | $this->_wrap_code_refs(\&_wrap_in_eval, @ret) | |||||||||||||||||||||||||||
| 75 | 8 | }; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 13 | 1; | |||||||||||||||||||||||||||