| 1 | package Eval::Safe; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use 5.022; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use Eval::Safe::Eval; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 13 | ▶ | use Eval::Safe::Safe; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use List::Util qw(none); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 13 | ▶ | use Scalar::Util qw(reftype refaddr); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 13 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 100 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 100 | my ($class, %options) = @_; | |||||||||||||||||||||||||||
| 17 | 100 | ▶ | croak "Eval::Safe->new called with invalid class name: $class" unless $class eq 'Eval::Safe'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 100 | my @known_options = qw(safe strict warnings debug package force_package); | |||||||||||||||||||||||||||
| 19 | 100 | ▶ | my @unknown_options = grep {my $k = $_; none { $k eq $_ } @known_options } keys %options; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 100 | ▶ | if (@unknown_options) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 1 | croak "Unknown options: ".join(' ', @unknown_options); | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | 99 | $options{strict} = _make_pragma('strict', $options{strict}); | |||||||||||||||||||||||||||
| 24 | 99 | $options{warnings} = _make_pragma('warnings', $options{warnings}); | |||||||||||||||||||||||||||
| 25 | 99 | ▶ | if ($options{package} and not $options{force_package}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$options{"package"} and not $options{"force_package"}
MC/DC100%2/2
$options{"package"} and not $options{"force_package"}
$options{"package"}
not $options{"force_package"}
Truth table100%3/3
A and not(B)
A= $options{"package"}B= not $options{"force_package"}
| |||||||||||||||||||||||||||||
| 26 | 10 | $options{package} = Eval::Safe::_validate_package_name($options{package}); | |||||||||||||||||||||||||||
| 27 | 10 | ▶ | croak "Package $options{package} already exists" if eval "%$options{package}::"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | 97 | ▶ | if ($options{safe} // 0 > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$options{"safe"} //
MC/DC100%1/1
$options{"safe"} //
$options{"safe"}
Truth table100%2/2
A
A= $options{"safe"}
| |||||||||||||||||||||||||||||
| 30 | 48 | return Eval::Safe::Safe->new(%options); | |||||||||||||||||||||||||||
| 31 | } else { | ||||||||||||||||||||||||||||
| 32 | 49 | return Eval::Safe::Eval->new(%options); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 54 | ▶ | sub package { | ||||||||||||||||||||||||||
|
Subroutine
package: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 54 | my ($this) = @_; | |||||||||||||||||||||||||||
| 38 | 54 | return $this->{package}; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 14 | ▶ | sub wrap { | ||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 14 | my ($this, $code) = @_; | |||||||||||||||||||||||||||
| 43 | 14 | return $this->eval("sub { ${code} }"); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 2 | ▶ | sub share { | ||||||||||||||||||||||||||
|
Subroutine
share: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 2 | my ($this, @vars) = @_; | |||||||||||||||||||||||||||
| 48 | 2 | my $calling_package = caller; | |||||||||||||||||||||||||||
| 49 | 2 | $this->share_from($calling_package, @vars); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 22 | ▶ | sub share_from { | ||||||||||||||||||||||||||
|
Subroutine
share_from: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 22 | my ($this, $package, @vars) = @_; | |||||||||||||||||||||||||||
| 54 | 22 | $package = _validate_package_name($package); | |||||||||||||||||||||||||||
| 55 | 20 | ▶ | croak "Package $package does not exist" unless eval "%${package}::"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 18 | for my $v (@vars) { | |||||||||||||||||||||||||||
| 57 | 22 | ▶ | croak "Variable has no leading sigil: $v" unless $v =~ m'^([&*$%@])(\w+)$'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 22 | my ($sigil, $symbol) = ($1, $2); | |||||||||||||||||||||||||||
| 59 | # There are only 5 different sigils, so we could skip the eval here and | ||||||||||||||||||||||||||||
| 60 | # instead branch on the $sigil and use a syntax like the one on the left of | ||||||||||||||||||||||||||||
| 61 | # the equal (e.g. \&{$package."::$symbol"}). See: | ||||||||||||||||||||||||||||
| 62 | # https://metacpan.org/source/MICB/Safe-b2/Safe.pm | ||||||||||||||||||||||||||||
| 63 | 13 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 22 | *{($this->package())."::${symbol}"} = eval "\\${sigil}${package}::${symbol}"; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 18 | ▶ | sub var_ref { | ||||||||||||||||||||||||||
|
Subroutine
var_ref: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 18 | my ($this, $var) = @_; | |||||||||||||||||||||||||||
| 70 | 18 | ▶ | croak "Variable has no leading sigil: $var" unless $var =~ m'^([&*$%@])(\w+)$'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | # There are only 5 different sigils, so we could skip the eval here and | ||||||||||||||||||||||||||||
| 72 | # instead branch on the $sigil. See: | ||||||||||||||||||||||||||||
| 73 | # https://metacpan.org/source/MICB/Safe-b2/Safe.pm | ||||||||||||||||||||||||||||
| 74 | 13 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | 18 | return eval sprintf '\%s%s::%s', $1, $this->package(), $2; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 8 | ▶ | sub interpolate { | ||||||||||||||||||||||||||
|
Subroutine
interpolate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 8 | my ($this, $str) = @_; | |||||||||||||||||||||||||||
| 80 | # It's not clear if Text::Balanced could help here. | ||||||||||||||||||||||||||||
| 81 | 8 | my $r = $this->eval("<<\"EVAL_SAFE_EOF_WORD\"\n${str}\nEVAL_SAFE_EOF_WORD\n"); | |||||||||||||||||||||||||||
| 82 | 8 | $r =~ s/\n$//; | |||||||||||||||||||||||||||
| 83 | 8 | return $r; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # _make_pragma('pragma', $arg) | ||||||||||||||||||||||||||||
| 87 | # Returns a string saying "no pragma" if $arg is false, "use pragma" if arg is | ||||||||||||||||||||||||||||
| 88 | # a `true` scalar, "use pragma $$arg" if arg is a scalar reference, and | ||||||||||||||||||||||||||||
| 89 | # "use pragma @$arg" if arg is an array reference. | ||||||||||||||||||||||||||||
| 90 | 198 | ▶ | sub _make_pragma() { | ||||||||||||||||||||||||||
|
Subroutine
_make_pragma: called CC=6 SCAR=24.8
| |||||||||||||||||||||||||||||
| 91 | 198 | my ($pragma, $arg) = @_; | |||||||||||||||||||||||||||
| 92 | 198 | my $reftype = reftype $arg; | |||||||||||||||||||||||||||
| 93 | 198 | ▶ | if (not defined $reftype) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 94 | 198 | ▶ | if ($arg) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 2 | return "use ${pragma};"; | |||||||||||||||||||||||||||
| 96 | } else { | ||||||||||||||||||||||||||||
| 97 | 196 | return "no ${pragma};"; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } elsif ($reftype eq 'SCALAR') { | ||||||||||||||||||||||||||||
| 100 | 0 | return "use ${pragma} '$arg';"; | |||||||||||||||||||||||||||
| 101 | } elsif ($reftype eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 102 | # We should probably use Data::Dumper to format the arg list properly in | ||||||||||||||||||||||||||||
| 103 | # case some of the args contain a space. | ||||||||||||||||||||||||||||
| 104 | 0 | return ("use ${pragma} qw(".join(' ', @$arg).');'); | |||||||||||||||||||||||||||
| 105 | } elsif ($reftype eq 'HASH') { | ||||||||||||||||||||||||||||
| 106 | 0 | return ("use ${pragma} qw(".join(' ', %$arg).');'); | |||||||||||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||||||||||
| 108 | 0 | croak "Invalid argument for '${pragma}' option, expected a scalar or array reference"; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # $safe->_wrap_code_refs('sub', @objects) | ||||||||||||||||||||||||||||
| 113 | # will call $safe->sub($ref) for all code references found within @objects and | ||||||||||||||||||||||||||||
| 114 | # store the result in place in @objects. The passed objects are crawled | ||||||||||||||||||||||||||||
| 115 | # recursively. | ||||||||||||||||||||||||||||
| 116 | # Finally, the modified array is returned. | ||||||||||||||||||||||||||||
| 117 | # | ||||||||||||||||||||||||||||
| 118 | # This is similar to the wrap_code_refs_within method in Safe. | ||||||||||||||||||||||||||||
| 119 | 54 | ▶ | sub _wrap_code_refs { | ||||||||||||||||||||||||||
|
Subroutine
_wrap_code_refs: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 120 | 54 | my ($this, $wrapper) = splice @_, 0, 2; | |||||||||||||||||||||||||||
| 121 | # We need to use @_ below (without giving it a new name) to retain its | ||||||||||||||||||||||||||||
| 122 | # aliasing property to modify the arguments in-place. | ||||||||||||||||||||||||||||
| 123 | 54 | my %seen_refs = (); | |||||||||||||||||||||||||||
| 124 | 54 | ▶ | my $crawler = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=21.8
| |||||||||||||||||||||||||||||
| 125 | 54 | for my $item (@_) { | |||||||||||||||||||||||||||
| 126 | 43 | my $reftype = reftype $item; | |||||||||||||||||||||||||||
| 127 | 43 | ▶ | next unless $reftype; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 8 | ▶ | next if ++$seen_refs{refaddr $item} > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 8 | ▶ | if ($reftype eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 130 | 0 | __SUB__->(@$item); # __SUB__ is the current sub. | |||||||||||||||||||||||||||
| 131 | } elsif ($reftype eq 'HASH') { | ||||||||||||||||||||||||||||
| 132 | 0 | __SUB__->(values %$item); | |||||||||||||||||||||||||||
| 133 | } elsif ($reftype eq 'CODE') { | ||||||||||||||||||||||||||||
| 134 | 8 | $item = $this->$wrapper($item); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | # We're ignoring the GLOBs for the time being. | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 54 | }; | |||||||||||||||||||||||||||
| 139 | 54 | $crawler->(@_); | |||||||||||||||||||||||||||
| 140 | 54 | ▶ | if (defined wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 38 | ▶ | return (wantarray) ? @_ : $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 16 | return; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # _validate_package_name('package::name') | ||||||||||||||||||||||||||||
| 147 | # Croaks (dies) if the given package name does not look like a package name. | ||||||||||||||||||||||||||||
| 148 | # Otherwise returns a cleaned form of the package name (trailing '::' are | ||||||||||||||||||||||||||||
| 149 | # removed, and '' or '::' is made into 'main'). | ||||||||||||||||||||||||||||
| 150 | 32 | ▶ | sub _validate_package_name { | ||||||||||||||||||||||||||
|
Subroutine
_validate_package_name: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 151 | 32 | my ($p) = @_; | |||||||||||||||||||||||||||
| 152 | 32 | $p =~ s/::$//; | |||||||||||||||||||||||||||
| 153 | 32 | ▶ | $p = 'main' if $p eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 32 | ▶ | croak "${p} does not look like a package name" unless $p =~ m/^\w+(::\w+)*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 30 | return $p; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 13 | 1; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | __DATA__ | ||||||||||||||||||||||||||||