File Coverage

blib/lib/ExtUtils/Scriptlet.pm
Criterion Covered Total %
statement 29 29 100.0
branch 14 16 87.5
condition 8 10 80.0
subroutine 6 6 100.0
pod 1 1 100.0
total 58 62 93.5


line stmt bran cond sub pod time code
1 1     1   24671 use strict;
  1         4  
  1         53  
2 1     1   10 use warnings;
  1         3  
  1         124  
3              
4             package ExtUtils::Scriptlet;
5              
6             our $VERSION = '1.132640'; # VERSION
7              
8             # ABSTRACT: run perl code in a new process without quoting it, on any OS
9              
10             #
11             # This file is part of ExtUtils-Scriptlet
12             #
13             #
14             # Christian Walde has dedicated the work to the Commons by waiving all of his
15             # or her rights to the work worldwide under copyright law and all related or
16             # neighboring legal rights he or she had in the work, to the extent allowable by
17             # law.
18             #
19             # Works under CC0 do not require attribution. When citing the work, you should
20             # not imply endorsement by the author.
21             #
22              
23 1     1   6 use Exporter 'import';
  1         1  
  1         36  
24 1     1   1556 use Data::Dumper;
  1         12663  
  1         617  
25              
26             our @EXPORT_OK = qw( perl );
27              
28              
29             sub perl {
30 14     14 1 17839 my ( $code, %p ) = @_;
31              
32 14 100       646 die "No code given" if !$code;
33              
34             # no idea why it needs 3, please send a letter if you know, so i can burn it
35 13 50       135 $code =~ s/\r\n/\r\r\r\n/g if $^O eq "MSWin32";
36              
37 13 100 100     138 die "at_argv needs to be an array reference" if $p{at_argv} and "ARRAY" ne ref $p{at_argv};
38 12 100 50     141 $p{at_argv} =
39             !defined $p{at_argv}
40             ? ""
41             : sprintf "\@ARGV = \@{ %s };",
42             Data::Dumper->new( [ $p{at_argv} || [] ] )->Useqq( 1 )->Indent( 0 )->Dump;
43              
44 12   66     505 $p{perl} ||= $^X;
45 12   100     195 $p{encoding} = sprintf ":encoding(%s)", $p{encoding} || "UTF-8";
46 12 100       387 $p{$_} = defined $p{$_} ? $p{$_} : "" for qw( args argv payload );
47              
48             open #
49 1 100   1   59 my $fh, #
  1         13  
  1         168  
  12         87375  
50             "|- :raw $p{encoding}", # :raw protects the payload from write
51             # mangling (newlines)
52             "$p{perl} $p{args} - $p{argv}" #
53             or die "cannot open perl: $!"; #
54              
55 11 50       840690 print $fh #
56             "$p{at_argv};" #
57             . "binmode STDIN;" # protect the payload from read
58             # mangling (newlines, system locale)
59             . "$code;" # unpack and execute serialized code
60             . "\n__END__\n" #
61             . $p{payload} #
62             or die "cannot print code to perl: $!"; #
63              
64 11         80 eval {
65 11         353 local $SIG{PIPE} = 'IGNORE'; # prevent the host perl from being
66             # terminated if the child perl dies
67 11 100       51871 close $fh or die "cannot close perl: $!"; # grab exit value so we can return it
68             };
69              
70 11         1214 return $?;
71             }
72              
73             1;
74              
75              
76             __END__