blib/lib/Env/Modify.pm

filter: Statementstmt 88.1% 156 / 177 Branchbran 65.6% 42 / 64 Conditioncond 61.1% 11 / 18 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 91.6% 22 / 24 Podpod 100% 8 / 8 totaltotal 80.7% 243 / 301 CC 41 SCAR 22.2
CC
41
Cov
81%
_sh_quote
19.6
_readpipe_mod_env
12.9
import
11.0
CRAP 9.2
↑ Summary
1 package Env::Modify;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Shell::GetEnv '0.09';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use String::ShellQuote; # nah, just borrow from String::ShellQuote
7 7 ▶use File::Spec::Functions 'catfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 *DEBUG = *DEVNULL; open DEVNULL,'>/dev/null';
11 7 ▶our $DEBUG && (*DEBUG = *STDERR);
Branch: 1 branch
BranchTrueFalseTotal
our $DEBUG and *DEBUG = *STDERR 0 7 7
12 7 our $SHELL = 'sh';
13 7 our $CHDIR = 0;
14  
15 7 our $VERSION = '0.02';
16 7 our @EXPORT_OK = qw(system readpipe qx backticks source
17 readpipe_list qx_list backticks_list);
18 7 my @readpipe_exp = qw(readpipe readpipe_list qx_list backticks_list);
19 7 our %EXPORT_TAGS = ('system' => ['system'],
20 'readpipe' => [@readpipe_exp],
21 'qx' => ['qx','backticks', @readpipe_exp],
22 'backticks' => ['qx','backticks', @readpipe_exp],
23 'all' => [ 'system', 'qx','backticks', 'source',
24 @readpipe_exp],
25 'chdir' => [],
26 'sh' => [], 'bash' => [], 'csh' => [], 'dash' => [],
27 'ksh' => [], 'tcsh' => [], 'zsh' => [] );
28  
29  
30 # default attributes that go to all Shell::GetEnv constructors
31 7 our %CMDOPT = (
32 startup => 0,
33 login => 0,
34 interactive => 0,
35 verbose => 0,
36 );
37  
38 7 our %ENVSOPT = (
39 ZapDeleted => 1
40 );
41  
42 7 our $TEMPDIR = File::Temp::tempdir( CLEANUP => 1 );
43  
44 7 my $calls = 0;
45  
46 # RT115330 describes a bug with overloaded readpipe function.
47 # It was fixed in Perl v5.20.0
48 7 our $_RT115330 = $] < 5.020000;
49  
50 7 ▶sub import {
Subroutine import: called CC=11 SCAR=24.0
51 # Handle the CORE::GLOBAL::readpipe bug (RT#115330) differently depending
52 # on whether the caller primarily uses qx(), `backticks`, or readpipe.
53 # The bug was fixed in Perl v5.20.0
54 7 my %tags = map {; $_ => 1 } grep /^:/, @_;
55 7 my $cgreadpipe = 0;
56 7 ▶ if ($tags{":qx"} || $tags{":backticks"} || $tags{":all"}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($tags{':qx'} or $tags{':backticks'} or $tags{':all'}) { } 2 5 7
elsif ($tags{':readpipe'}) { } 1 4 5
Condition83%5/6
$tags{":qx"} or $tags{":backticks"} or $tags{":all"}
l!l&&r!l&&!r
205
$tags{":qx"} or $tags{":backticks"}
l!l&&r!l&&!r
115
MC/DC66%2/3
$tags{":qx"} or $tags{":backticks"} or $tags{":all"}
$tags{":qx"} $tags{":backticks"} $tags{":all"}
Truth table75%3/4
A or B or C
A= $tags{":qx"}B= $tags{":backticks"}C= $tags{":all"}
ABCresult
1XX1
01X1
0011
0000
57 6 ▶ *CORE::GLOBAL::readpipe = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
58 6 local $_RT115330 = $] < 5.020000;
59 6 return _readpipe_mod_env(@_);
60 2 };
61 2 $cgreadpipe = 1;
62 } elsif ($tags{":readpipe"}) {
63 # NB: CORE::GLOBAL::readpipe will not be respected for Perl <=v5.8.9
64 7 ▶ *CORE::GLOBAL::readpipe = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
65 7 local $_RT115330 = 0;
66 7 return _readpipe_mod_env(@_);
67 1 };
68 1 $cgreadpipe = 1;
69 }
70  
71 7 ▶ if ($tags{":system"} || $tags{":all"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tags{":system"} or $tags{":all"}) 2 5 7
Condition66%2/3
$tags{":system"} or $tags{":all"}
l!l&&r!l&&!r
205
MC/DC50%1/2
$tags{":system"} or $tags{":all"}
$tags{":system"} $tags{":all"}
Truth table66%2/3
A or B
A= $tags{":system"}B= $tags{":all"}
ABresult
1X1
011
000
72 7 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
73 2 *CORE::GLOBAL::system = \&Env::Modify::system;
74 }
75 7 ▶ if ($tags{":chdir"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tags{":chdir"}) 1 6 7
76 1 $CHDIR = 1;
77 }
78 7 for my $shell ('zsh','tcsh','ksh','dash','csh','bash','sh') {
79 49 ▶ if ($tags{":$shell"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tags{":$shell"}) 0 49 49
80 0 $SHELL = $shell;
81 }
82 }
83 7 ▶ if ($cgreadpipe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cgreadpipe) { } 3 4 7
84 3 __PACKAGE__->export_to_level(1, __PACKAGE__, @_[1..$#_]);
85 3 my $callpkg = caller;
86 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
87 7 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
88 3 *{$callpkg . "::readpipe"} = \&CORE::GLOBAL::readpipe;
89 } else {
90 4 goto &Exporter::import;
91 }
92 }
93  
94 4 ▶sub backticks {goto &readpipe }
Subroutine backticks: called CC=1 SCAR=0.0
Pod documented
95 3 ▶sub qx { goto &readpipe }
Subroutine qx: called CC=1 SCAR=0.0
Pod documented
96  
97 0 ▶sub backticks_list { goto &readpipe_list }
Subroutine backticks_list: not called CC=1 SCAR=6.9
Pod documented
98 0 ▶sub qx_list { goto &readpipe_list }
Subroutine qx_list: not called CC=1 SCAR=6.9
Pod documented
99  
100 7 ▶sub readpipe {
Subroutine readpipe: called CC=1 SCAR=0.0
Pod documented
101 7 local $_RT115330 = 0;
102 7 return _readpipe_mod_env(@_);
103 }
104  
105 1 ▶sub readpipe_list {
Subroutine readpipe_list: called CC=2 SCAR=7.1
Pod documented
106 1 local $_RT115330 = 0;
107 1 ▶ my $cmd = @_ > 1 ? _sh_quote(@_) : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 1 0 1
108 1 return _readpipe_mod_env($cmd);
109 }
110  
111 35 ▶sub system {
Subroutine system: called CC=5 SCAR=16.1
Pod documented
112 35 my @cmd = @_;
113 35 ▶ my $cmd = @cmd > 1 ? _sh_quote(@cmd) : $cmd[0];
Branch: 1 branch
BranchTrueFalseTotal
@cmd > 1 ? : 4 31 35
114 35 $calls++;
115 35 my $stub = "$calls.$$";
116 35 my $stdout = catfile( $TEMPDIR, $stub . "-stdout");
117 35 my $stderr = catfile( $TEMPDIR, $stub . "-stderr");
118 35 my $getenv = Shell::GetEnv->new( $SHELL, $cmd,
119 { %CMDOPT, echo => 0,
120 stdout => $stdout, stderr => $stderr } );
121 26 $getenv->import_envs( %ENVSOPT );
122  
123 26 ▶ if (-s $stderr) {
Branch: 1 branch
BranchTrueFalseTotal
if (-s $stderr) 6 20 26
124 6 open my $err, '>&2'; # or warn "print to unopened filehandle?"
125 6 open my $fh, '<', $stderr;
126 6 print {$err} <$fh>;
127 6 close $fh;
128 6 close $err;
129 }
130 26 ▶ if (-s $stdout) {
Branch: 1 branch
BranchTrueFalseTotal
if (-s $stdout) 4 22 26
131 4 open my $out, '>&1'; # or warn "print to unopened filehandle?"
132 4 open my $fh, '<', $stdout;
133 4 print {$out} <$fh>;
134 4 close $fh;
135 4 close $out;
136 }
137 26 unlink $stdout, $stderr;
138 26 my $status = _status($getenv->status);
139 26 ▶ $CHDIR && chdir $ENV{PWD};
Branch: 1 branch
BranchTrueFalseTotal
$Env::Modify::CHDIR and chdir $ENV{"PWD"} 3 23 26
140 26 return $? = $status;
141 }
142  
143 3 ▶sub source {
Subroutine source: called CC=4 SCAR=20.1
Pod documented
144 3 my ($file,@args) = @_;
145 3 ▶ if ($SHELL eq 'tcsh') {
Branch: 3 branches
BranchTrueFalseTotal
if ($Env::Modify::SHELL eq 'tcsh') { } 0 3 3
elsif ($Env::Modify::SHELL eq 'csh') { } 0 3 3
elsif ($Env::Modify::SHELL eq 'ksh') { } 0 3 3
146 0 $file = _sh_quote($file,@args);
147 0 return Env::Modify::system( "source $file" );
148 } elsif ($SHELL eq 'csh') {
149 0 $file = _sh_quote($file,@args);
150 0 return Env::Modify::system("source $file");
151 } elsif ($SHELL eq 'ksh') {
152 # . $file is supposed to work, but it doesn't
153 0 $file = _sh_quote($file,@args);
154 0 return Env::Modify::system("eval \$(cat $file)");
155 } else {
156 3 return Env::Modify::system( ". " . _sh_quote($file,@args) );
157 }
158 }
159  
160 21 ▶sub _readpipe_mod_env {
Subroutine _readpipe_mod_env: called CC=10 SCAR=25.6
161 21 my ($cmd) = @_;
162  
163 21 ▶ if ($_RT115330) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Env::Modify::_RT115330) 0 21 21
164 0 ▶ if ($cmd !~ /"/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cmd =~ /"/) { } 0 0 0
165 0 $cmd = eval qq["$cmd"];
166 } else {
167 0 for my $delim ('#',',', qw(! ' ; " @ $ % ^ & * - = +
168 : . / ? ~ ` 00)) {
169 0 ▶ if ($cmd eq '00') {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmd eq '00') { } 0 0 0
elsif (not $cmd =~ /\Q$delim\E/) { } 0 0 0
170 0 warn __PACKAGE__,"::_readpipe_mod_env: ",
171 "reinterpolation of $cmd neglected";
172 } elsif ($cmd !~ /\Q$delim/) {
173 0 $cmd = eval qq[qq$delim$cmd$delim];
174 0 last;
175 }
176 }
177 }
178 }
179 21 $calls++;
180 21 my $stub = "$calls.$$";
181 21 my $stdout = catfile( $TEMPDIR, $stub . "-stdout");
182 21 my $stderr = catfile( $TEMPDIR, $stub . "-stderr");
183 21 my $getenv = Shell::GetEnv->new( $SHELL, $cmd,
184 { %CMDOPT, echo => 0,
185 stdout => $stdout, stderr => $stderr } );
186 21 $getenv->import_envs( %ENVSOPT );
187 21 my @out;
188 {
189 21 ▶ local $/ = wantarray ? $/ : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 21 21
190 7 ▶ no warnings 'io'; # in case open $fh touches fd 1 or 2
Subroutine BEGIN: called CC=1 SCAR=0.0
191 21 open my $fh, '<', $stdout;
192 21 @out = <$fh>;
193 21 close $fh;
194 21 ▶ if (-s $stderr) {
Branch: 1 branch
BranchTrueFalseTotal
if (-s $stderr) 9 12 21
195 9 open my $err, '>&=2'; # or warn "print to unopened filehandle?"
196 9 open my $fh, '<', $stderr;
197 9 my @err = <$fh>;
198 9 print {$err} @err;
199 9 close $err;
200 9 close $fh;
201 }
202 }
203 21 unlink $stdout, $stderr;
204 21 ▶ $CHDIR && chdir $ENV{PWD};
Branch: 1 branch
BranchTrueFalseTotal
$Env::Modify::CHDIR and chdir $ENV{"PWD"} 1 20 21
205 21 $? = _status($getenv->status);
206 21 ▶ return wantarray ? @out : $out[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 21 21
207 }
208  
209 47 ▶sub _status {
Subroutine _status: called CC=2 SCAR=6.9
210 47 my $ge_status = shift;
211  
212 # http://tldp.org/LDP/abs/html/exitcodes.html
213 # treat exit code 128+n as command terminated by signal <n> ?
214 # may not be portable to non-POSIX OS or other shells
215 47 my $status = $ge_status;
216 47 ▶ if ($status > 128) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status > 128) { } 1 46 47
217 1 $status = $status - 128;
218 } else {
219 46 $status = $status << 8;
220 }
221 }
222  
223 12 ▶sub _sh_quote { # borrowed heavily from String::ShellQuote 1.04
Subroutine _sh_quote: called CC=13 SCAR=29.8
224 12 my @in = @_;
225 12 ▶ return '' unless @in;
Branch: 1 branch
BranchTrueFalseTotal
unless @in 0 12 12
226 12 my ($ret, $saw_non_equal, @err) = ('', 0);
227 12 foreach (@in) {
228 37 ▶ if (!defined $_ or $_ eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_ or $_ eq "") 0 37 37
Condition33%1/3
not defined $_ or $_ eq ""
l!l&&r!l&&!r
0037
MC/DC0%0/2
not defined $_ or $_ eq ""
not defined $_ $_ eq ""
Truth table33%1/3
not(A) or B
A= not defined $_B= $_ eq ""
ABresult
1X1
011
000
229 0 $_ = "''";
230 0 next;
231 }
232 s/\x00//g &&
233 37 ▶ push @err, "No way to quote string containing null (\\000) bytes";
Branch: 1 branch
BranchTrueFalseTotal
s/\x00//g and push @err, "No way to quote string containing null (\\000) bytes" 0 37 37
234 37 my $escape = 0;
235 37 ▶ if (/=/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/=/) { } 0 37 37
236 0 ▶ $escape = 1 if !$saw_non_equal;
Branch: 1 branch
BranchTrueFalseTotal
unless $saw_non_equal 0 0 0
237 } else {
238 37 $saw_non_equal = 1;
239 }
240 37 ▶ $escape = 1 if m|[^\w!%+,\-./:=@^]|;
Branch: 1 branch
BranchTrueFalseTotal
if m[[^\w!%+,\-./:=\@^]] 12 25 37
241 37 ▶ if ($escape || (!$saw_non_equal && /=/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($escape or not $saw_non_equal and /=/) 12 25 37
Condition50%3/6
$escape or not $saw_non_equal and /=/
l!l&&r!l&&!r
12025
not $saw_non_equal and /=/
!ll&&!rl&&r
2500
MC/DC33%1/3
$escape or not $saw_non_equal and /=/
$escape not $saw_non_equal /=/
Truth table50%2/4
A or not(B) and C
A= $escapeB= not $saw_non_equalC= /=/
ABCresult
1XX1
0111
00X0
0100
242 12 s/'/'\\''/g; # ' -> '\''
243  
244 # make multiple ' in a row look simpler
245 # '\'''\'''\'' -> '"'''"'
246 0 s|((?:'\\''){2,})|q{'"} . (q{'} x (length($1) / 4)) . q{"'}|ge;
247 12 $_ = "'$_'";
248 12 s/^''//;
249 12 s/''$//;
250 }
251 } continue {
252 37 $ret .= "$_ ";
253 }
254 12 chop $ret;
255 12 ▶ if (@err) {
Branch: 1 branch
BranchTrueFalseTotal
if (@err) 0 12 12
256 0 warn __PACKAGE__ . "::sh_quote: @err";
257 }
258 12 return $ret;
259 }
260  
261 7 1;
262  
263 =head1 NAME
264  
265 Env::Modify - affect Perl %ENV from subshell
266  
267 =head1 VERSION
268  
269 0.02
270  
271 =head1 SYNOPSIS
272  
273 use Env::Modify 'system', ':readpipe';
274  
275 $ENV{FOO}="bar";
276 system('echo $FOO'); # "bar"
277 system('FOO=baz');
278 print $ENV{FOO}; # "baz"
279  
280 # on Perl <=v5.8.8, say "Env::Modify::readpipe" instead
281 $out = qx(BAR=123; export BAR; echo hello);
282 print $ENV{BAR}; # "123"
283 print $out; # "hello";
284  
285 ###
286  
287 use Env::Modify 'source', ':bash', ':chdir';
288 open ENV, '>my_env.sh';
289 print ENV "export MEANING_OF_LIFE=42\n";
290 print ENV "cd \$HOME\n";
291 print ENV "cd .cpan\n";
292 close ENV;
293 my $status = source("my_env.sh");
294 print $ENV{MEANING_OF_LIFE}; # "42"
295 print "Current dir: ",Cwd::getcwd(),"\n"; # "/home/mob/.cpan"
296  
297  
298 =head1 DESCRIPTION
299  
300 New Perl programmers are often confused about how the C<system> call
301 interacts with the environment, and they wonder why this code:
302  
303 system('export foo=bar');
304 system('echo $foo');
305  
306 behaves differently from
307  
308 system('export foo=bar; echo $foo');
309  
310 or why when they run this code
311  
312 system("chdir \$HOME/scripts");
313 system("source my.env");
314 system("./my_script.sh");
315  
316 all the environment variables that they carefully set in C<my.env>
317 are ignored by C<./my_script.sh>.
318  
319 The reason these codes do not meet the new user's expectations
320 is that subshells, such as those launched by C<system>, receive
321 their own copy of the operating system environment and can only
322 make changes to that local environment.
323  
324 This module seeks to overcome that limitation, allowing
325 C<system> calls (and C<qx()>/C<readpipe>/backticks) calls
326 to affect the local environment. It uses the clever mechanism
327 in Diab Jerius's L<Shell::GetEnv|Shell::GetEnv> module to copy
328 the environment of the subshell back to the calling environment.
329  
330 =head1 FUNCTIONS
331  
332 =head2 system
333  
334 =head2 EXIT_CODE = system LIST
335  
336 Acts like the builtin L<perlfunc/"system"> command,
337 but any changes made to the subshell environment are preserved
338 and copied to the calling environment.
339  
340 Like the builtin call, the return value is the exit status of
341 the command as returned by the L<perlfunc/"wait"> call.
342  
343 When you import the C<system> command into your calling
344 package (with C<use Env::Modify ':system'>),
345 C<Env::Modify> installs the C<Env::Modify::system>
346 function to the C<CORE::GLOBAL> namespace (see
347 L<perlsub/"Overriding Build-in Functions">), making it
348 available anywhere that your program makes a call to C<system>.
349  
350 =head2 readpipe
351  
352 =head2 readpipe(EXPR)
353  
354 =head2 Env::Modify::qx(EXPR), &qx(EXPR)
355  
356 =head2 backticks(EXPR)
357  
358 Executes a system command and returns the standard output of
359 the command. In scalar context, the output comes back as a single
360 (potentially multi-line) string. In list context, returns a list
361 of lines (however lines are defined with L<perlvar/"$/"> or
362 L<perlvar/"$INPUT_RECORD_SEPARATOR">).
363  
364 Unlike the builtin C<readpipe> command, any changes made by the
365 system command to the subshell environment are preserved and
366 copied back to the calling environment.
367  
368 When any of the functions C<readpipe>, C<qx>, or C<backticks>
369 or any of the tags C<:readpipe>, C<:qx>, C<:backticks>, or
370 C<:all> are imported into the calling namespace,
371 this module installs the C<Env::Modify::readpipe> function to
372 the C<CORE::GLOBAL> namespace. As described in
373 L<perlsub/"Overriding Built-in Functions">, an override
374 for the C<readpipe> function also overrides the operators
375 C<``> and C<qx{}>. Note that C<readpipe> was not supported
376 as an overridable function for the C<CORE::GLOBAL> package
377 until Perl v5.8.9. If your version of perl is older than that,
378 you will need to use function names and not C<qx[]> or
379 backticks notation to get this module to modify your
380 environment.
381  
382 See the L<"RT115330"> section for another important caveat
383 about the C<readpipe> set of functions, and how to structure
384 your C<use Env::Modify ...> statement to make best use of
385 this module.
386  
387 =head2 readpipe_list LIST
388  
389 =head2 backticks_list LIST
390  
391 =head2 qx_list LIST
392  
393 Convenience functions to accommodate external commands with
394 shell metacharacters. Like the L<"readpipe"> function,
395 but may take a list of arguments the way that Perl's
396 C<system LIST> function does. Compare:
397  
398 $output = readpipe("ls -l \"My Documents\" Videos*");
399 $output = readpipe_list("ls","-l","My Documents","Videos*");
400  
401 (See also:
402 L<https://metacpan.org/pod/distribution/perl/Porting/todo.pod#readpipe-LIST>.)
403  
404 =head2 source FILE LIST
405  
406 Like the shell built-in command of the same name (also called
407 C<.> in some shells), executes the shell
408 commands in a file and incorporates any modifications to the
409 subshell's environment into the calling environment.
410  
411 That is, if C<my_env.sh> contains
412  
413 FOO=123
414 NUM_HOME_FILES=$(ls $HOME | wc -l)
415 export FOO NUM_HOME_FILES
416  
417 then you could run
418  
419 use Env::Modify 'source';
420 source("my_env.sh");
421 print "FOO is $ENV{FOO}\n"; # expect: "123"
422 print "There are $ENV{NUM_HOME_FILES} in the home dir\n";
423  
424 =head1 RT115330
425  
426 L<"perlbug RT#115330"|https://rt.perl.org/Ticket/Display.html?id=115330>
427 described an issue with Perl code that sets the C<*CORE::GLOBAL::readpipe>
428 function, like this module does.
429 This bug was fixed in Perl v5.20.0
430 and if you are not using an older version of Perl than that, you
431 are not affected by this bug and you may stop reading.
432  
433 The short version is that the arguments received by the
434 C<CORE::GLOBAL::readpipe> function are correct when you
435 use the keyword C<readpipe> to call the function, and they
436 need to be interpolated an extra time when you use backticks
437 or the C<qx//> construction.
438  
439 The form of the C<use> statement should resemble the way
440 that you I<usually> invoke the C<readpipe> command. If your
441 code literally calls the C<readpipe> function, then you
442 should load this module with
443  
444 use Env::Modify ':readpipe';
445  
446 If your code usually uses the C<qx!!> construct or backticks
447 to invoke C<readpipe>, then you should load the module with
448 either
449  
450 use Env::Modify ':qx';
451 use Env::Modify ':backticks';
452  
453 (these two calls are equivalent). This will have the effect
454 of interpolating the input one last time before the input
455 is passed to the shell.
456  
457 If your code uses both C<readpipe> and C<qx{}>/backticks,
458 you can always workaround this bug using fully-qualified function
459 names like C<Env::Modify::readpipe()>,
460 C<Env::Modify::qx()>, or
461 C<Env::Modify::backticks()>. All of these function calls
462 will receive correctly interpolated input.
463  
464 =head1 MODULE VARIABLES
465  
466 =head2 $SHELL
467  
468 The shell that the L<Shell::GetEnv> module will use to run
469 an external command. This must be a value supported by
470 L<Shell::GetEnv>, namely one of C<bash>, C<csh>, C<dash>,
471 C<ksh>, C<sh>, C<tcsh>, or C<zsh>.
472 Defaults to C<sh>.
473  
474 The value of C<$SHELL> can also be set by specifing a tag
475 with the shell name when this module is imported. For
476 example, to specify C<bash> as the shell for this module
477 to use, load this module like
478  
479 use Env::Modify ':bash';
480  
481 =head2 $CHDIR
482  
483 If C<$Env::Modify::CHDIR> is set to a true value, then any
484 change of the current working directory in a subshell will
485 also affect the working directory in the calling (Perl)
486 environment. That is, you can say
487  
488 chdir "/some/path";
489 $f = -f "bar"; # -f /some/path/bar
490 system("cd foo");
491 $g = -f "bar"; # -f /some/path/foo/bar
492  
493 You can also enable this feature at import time with the
494 C<:chdir> tag.
495  
496 =head2 %CMDOPT
497  
498 A set of options that are passed to the L<Shell::GetEnv>
499 constructor. See the C<new> method in L<Shell::GetEnv/"METHODS">.
500  
501 # don't load startup files (.profile, .bashrc, etc.)
502 # when system() runs below
503 local $Env::Modify::CMDOPT{startup} = 0;
504 Env::Modify::system("FOO=bar; export FOO");
505  
506  
507 =head2 %ENVSOPT
508  
509 A set of options that are passed to the C<import_envs>
510 method of L<Shell::GetEnv>. See the C<import_envs> method
511 in L<Shell::GetEnv/"METHODS">.
512  
513 # don't remove entries from Perl environment
514 local $Env::Modify::ENVSOPT{ZapDeleted} = 0;
515 source("./script_that_erases_PATH_var.sh");
516 print "PATH is still $ENV{PATH}"; # not erased
517  
518 =head1 EXPORT
519  
520 This module has four functions that can be exported into the
521 calling namespace: C<system>, C<readpipe>, C<qx>, and C<backticks>.
522 As C<qx> is a Perl language construction and not just a keyword,
523 if you import the C<qx> function you would either have to use
524 a fully qualified function name or a sigil to use it:
525  
526 package My::Pkg;
527 use Env::Modify 'qx';
528 ...
529 $out1 = qx($cmd1); # calls Perl built-in, not Env::Modify::qx !
530 $out2 = &qx($cmd2); # calls Env::Modify::qx
531 $out3 = My::Pkg::qx($cmd3); # calls Env::Modify::qx
532  
533 The tag C<:system> exports the C<system> function into the calling
534 namespace and also sets the C<CORE::GLOBAL::system> function, so that
535 all C<system> calls in any package in any part of your script will
536 use the C<Env::Modify::system> function.
537  
538 The tags C<:readpipe>, C<:qx>, or C<:backticks> export the
539 C<readpipe>, C<qx>, and C<backticks> functions into the calling
540 namespace, and also set the C<CORE::GLOBAL::readpipe> function,
541 so that all C<readpipe> calls, C<qx//> constructions, or
542 backticks expressions in any package and in any part of your script
543 will use the C<Env::Modify::readpipe> function. If you are
544 vulnerable to L<"RT115330"> (see above), then you should use
545 C<:readpipe> if your script generally uses C<readpipe()> to
546 capture output from external programs and use C<:qx> or C<:backticks>
547 if your script generally uses C<qx!!> or backticks.
548  
549 The C<:all> tag behaves like C<:system> + C<:backticks>.
550  
551 You may also specify the C<:chdir> tag to enable the "chdir"
552 feature (see C<$CHDIR> under L<"MODULE VARIABLES">), or a tag
553 with the name of a shell like C<:sh>, C<:bash>, etc.
554 to specify the default shell for this module to run external
555 commands (see C<$SHELL> under L<"MODULE VARIABLES">).
556  
557 =head1 LIMITATIONS
558  
559 =over 4
560  
561 =item Portability
562  
563 C<Env::Modify> can only work on systems where L<Shell::GetEnv>
564 will work, namely systems where POSIX-y type shells are
565 installed.
566  
567 =item Buffering
568  
569 With a regular C<system> or C<readpipe>/C<qx>/backticks call,
570 lines from the standard error stream of the external command
571 (and from the standard output stream in the case of C<system>)
572 are written to the terminal as the external program produces
573 them. Because of the nature of how this module recovers and
574 transfers the environment of the subshell, C<Env::Modify>
575 functions will hold onto external program output, and not
576 publish it to your Perl script's terminal until the command
577 has completed. This may cause
578 L<suffering from buffering|http://perl.plover.com/FAQs/Buffering.html>,
579 and for that, the author of this module apologizes.
580  
581 =item Interlaced standard output and standard error
582  
583 In a regular C<system> call that writes to both standard
584 output and standard error, lines from the output stream
585 and error stream will often be interleaved on your terminal.
586 Because of the nature of how this module recovers and
587 transfers the environment of the subshell, C<Env::Modify::system>
588 calls will not interleave error and output this way. All
589 of the standard error output, if any, will be written to
590 the terminal (file descriptor 2, which is usually but not
591 necessarily STDERR), followed by all standard output being
592 written to the terminal (file descriptor 1).
593  
594 =back
595  
596 =head1 DEPENDENCIES
597  
598 L<Shell::GetEnv> provides the mechanism for copying a subshell's
599 environment back into the calling environment.
600  
601 =head1 SUPPORT
602  
603 You can find documentation for this module with the perldoc command.
604  
605 perldoc Env::Modify
606  
607 You can also look for information at:
608  
609 =over 4
610  
611 =item * RT: CPAN's request tracker
612  
613 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Env-Modify>
614  
615 =item * AnnoCPAN: Annotated CPAN documentation
616  
617 L<http://annocpan.org/dist/Env-Modify>
618  
619 =item * CPAN Ratings
620  
621 L<http://cpanratings.perl.org/d/Env-Modify>
622  
623 =item * Search CPAN
624  
625 L<http://search.cpan.org/dist/Env-Modify/>
626  
627 =back
628  
629 =head1 AUTHOR
630  
631 Marty O'Brien, C<< <mob at cpan.org> >>
632  
633 =head1 COPYRIGHT AND LICENSE
634  
635 Copyright (c) 2016, Marty O'Brien
636  
637 This library is free software; you can redistribute it and/or modify
638 it under the same terms as Perl itself, either Perl version 5.8.8 or,
639 at your option, any later version of Perl 5 you may have available.
640  
641 See http://dev.perl.org/licenses/ for more information.
642  
643 =cut
644  
645 # TODO: open2, open3 that modifies environment
646 # test on openbsd, Cygwin, other systems
↑ Summary