Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: How to set (environment) variables for the script

by mr_mischief (Monsignor)
on Jun 10, 2010 at 13:48 UTC ( [id://844009]=note: print w/replies, xml ) Need Help??


in reply to How to set (environment) variables for the script

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://844009]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-24 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found