File Coverage

blib/lib/App/perlimports/Sandbox.pm
Criterion Covered Total %
statement 33 33 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 44 44 100.0


line stmt bran cond sub pod time code
1             package App::perlimports::Sandbox;
2              
3 85     85   2418 use strict;
  85         312  
  85         2611  
4 85     85   538 use warnings;
  85         227  
  85         3358  
5              
6             our $VERSION = '0.000052';
7              
8 78     78   35197 use Data::UUID ();
  78         49486  
  78         15183  
9              
10             {
11             my $du = Data::UUID->new;
12              
13             sub pkg_for {
14 189     189 1 587 my $module_name = shift;
15 189         13007 my $unique = 'A' . $du->create_str;
16 189         1346 $unique =~ s{-}{}g;
17              
18 188         2585 return sprintf(
19             'Local::%s::%s',
20             $module_name,
21             $unique
22             );
23             }
24             }
25              
26             sub eval_pkg {
27 99     99 1 18353 my $module_name = shift;
28 99         299 my $content = shift;
29              
30 97         368 my $pkg = pkg_for($module_name);
31              
32 97         490 my $to_eval = <<"EOF";
33             package $pkg;
34             $content;
35             1;
36             EOF
37              
38             ## no critic (Variables::RequireInitializationForLocalVars)
39 97         229 local $@;
40             ## no critic (BuiltinFunctions::ProhibitStringyEval,ErrorHandling::RequireCheckingReturnValueOfEval)
41 97     29   9366 eval $to_eval;
  29     22   252  
  29     10   115  
  29     7   1615  
  23         519  
  21         62  
  21         986  
  10         549  
  10         30  
  10         242  
  7         61  
  7         18  
  7         220  
42              
43 96         3269 my $e = $@;
44 96         686 return $e;
45             }
46              
47             1;
48              
49             # ABSTRACT: Internal Tools for perlimports
50              
51             __END__
52              
53             =pod
54              
55             =encoding UTF-8
56              
57             =head1 NAME
58              
59             App::perlimports::Sandbox - Internal Tools for perlimports
60              
61             =head1 VERSION
62              
63             version 0.000052
64              
65             =head2 pkg_for( $string )
66              
67             Returns a random module/package name, which can be used to eval arbitrary code.
68             Requires the name of the module which will be imported into the package to be
69             created.
70              
71             =head2 eval_pkg( $module_name, $pkg_content )
72              
73             Takes a module name and content to eval. Returns the contents of C<$@>. So, if
74             it returns true, the C<eval> failed.
75              
76             Returns a random module/package name, which can be used to eval arbitrary code.
77             Requires the name of the module which will be imported into the package to be
78             created.
79              
80             =head1 AUTHOR
81              
82             Olaf Alders <olaf@wundercounter.com>
83              
84             =head1 COPYRIGHT AND LICENSE
85              
86             This software is copyright (c) 2020 by Olaf Alders.
87              
88             This is free software; you can redistribute it and/or modify it under
89             the same terms as the Perl 5 programming language system itself.
90              
91             =cut