That actually works, but I think it'd be arguably cleaner if you're just setting variables in your shell script to write some Perl to parse the variables out of the script. Currently you're looking at one exec to get the Perl code running. If your environment isn't already set, you're looking at exec'ing another shell, which then reads your shell script for inclusion and execs another perl. It's conceptually pretty simple, but it's going to be tricky to follow in the process tree should you need to do so sometime.

Compare that to something like this:

if ( not defined $ENV{'foo'} ) { # foo being some variable already set by your shell # script rather than a separate flag open ( my $se, '<', './set_env.sh' ) or die "Can't read ./set_env.sh : $!\n"; while ( <$se> ) { next unless /(\w+)=["']?(\w+)["']?/; # handling exotic formatting left as an exercise $ENV{$1} = $2; } } print "foo is $ENV{'foo'}!\n";

If you're running programs from within your shell script to set the environment rather than just setting variables, then I'd stick with what you have. Execing a few more times is a drawback, but it's a tiny one. It would be preferable to additional maintenance if your variable parser got any fancier than the one above.

I still question the use of a separate flag variable to let you know the environment is set up even when using the shell, but that's more of a personal preference. Other things to consider are whether you have any variables that need to be set prior to perl launching rather than after and whether you're using any of the variables that perl would need to have set as environment variables rather than just reading them into a Perl option hash.


In reply to Re: How to set (environment) variables for the script by mr_mischief
in thread How to set (environment) variables for the script by Skeeve

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.