in reply to Re: system & exec
in thread system & exec
If the shell command you want to source sets some environment variables and you want to use them in your Perl script
I've been waiting for an excuse to post this little hack I used recently ;-) A Perl script that needs some environment variables set by a shell script can write another shell script that sets the variables and then re-runs the Perl script... (currently written to rely on *NIX and bash).
use warnings; use strict; use File::Temp qw/tempfile/; use Cwd qw/getcwd abs_path/; use String::ShellQuote qw/shell_quote/; my $SOURCE_FILE = '/tmp/test.sh'; if (!$ENV{PERL_REEXECUTED}) { my ($fh,$fn) = tempfile( DIR=>getcwd, SUFFIX=>'.sh' ); print $fh ". ", shell_quote($SOURCE_FILE), "\n"; print $fh "PERL_REEXECUTED=1 ", shell_quote($^X), " ", shell_quote(abs_path($0)), " \"\$\@\"\n"; print $fh "rm -f ",shell_quote($fn),"\n"; close $fh; exec('/bin/bash','--',$fn,@ARGV)==0 or die $?; }
|
|---|