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 $?; }

In reply to Re^2: system & exec by haukex
in thread system & exec by dideod.yang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.