Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

How to set (environment) variables for the script

by Skeeve (Vicar)
on Jun 10, 2010 at 13:04 UTC ( #843996=perlquestion: print w/replies, xml ) Need Help??
Skeeve has asked for the wisdom of the Perl Monks concerning the following question:

Hi fellow monks!

I have a shell script ( which sets some variables. It is usually sourced from other shell scripts.

Now I have a perl script which needs these settings too. So usually I call my script like this:

. ./ ./myscript

But I don't like this ;-) One way would be to use a wrapper script containing these 2 commands, but I don't like it too much.

So is there a way to get my script to call and use its settings?

My idea was to do it something like this (incomplete pseudocode) at the very beginning of the code of myscript

#!/usr/bin/perl if ( not defined $ENV{'environment_set'} ) { exec ". ./ ; environment_set=1 ./myscript"; } # script follows here

Is there a cleaner way? What do you suggest?


Replies are listed 'Best First'.
Re: How to set (environment) variables for the script
by mr_mischief (Monsignor) on Jun 10, 2010 at 13:48 UTC

    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, '<', './' ) or die "Can't read ./ : $!\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.

Re: How to set (environment) variables for the script
by walto (Pilgrim) on Jun 10, 2010 at 13:46 UTC
Re: How to set (environment) variables for the script
by Skeeve (Vicar) on Jun 10, 2010 at 14:58 UTC

    This gave me the cleanest solution, I guess: perldoc perlrun

    My script starts now like this:

    #!/bin/sh mydir=`dirname $0` . $mydir/ exec perl -x $0 "$@" #!perl use strict; use warnings; # and so on...

    Thanks to all who replied!

    Update: In reply to Arge1 below and because I noticed today That I myself needed parameters to my script.

    I appended "$@" to my exec above. This will append all parameters, individually quoted, to the exec command for my perl script to handle them.

Re: How to set (environment) variables for the script
by Argel (Prior) on Jun 10, 2010 at 21:49 UTC
    I usually use symlinks and a tcsh script like so:
    lrwxrwxrwx ... inet_audit -> inet_audit.csh* -rwx------ ... inet_audit.csh* -rwx------ ...*
    And the contents of inet_audit.csh are below. Note that the $argv:q is used to pass all parameters to the Perl script.
    #!/usr/bin/tcsh source /our/etc/perl5101env_script.csh ./ $argv:q
    The main I reason I do it this way is so that users, scripts, desktop environments (KDE and GNOME), etc. do not have to be modified if I want to change which script is used. If there were no environment issues then inet_audit would be symlink to This came about after we converted several .sh and .csh scripts to Perl but had to symlink the original names to the new Perl script. UGH!! This also makes it easier to consolidate scripts, simialr to busybox. Anyway, not going to claim it's perfect.

    I do kind of like the exec trick above, but how would I pass parameters in to the Perl script?

    Elda Taluta; Sarks Sark; Ark Arks

      See above for the change. Appending "$@" passes all parameters unchanged to the perl script.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://843996]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2018-03-20 11:47 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (250 votes). Check out past polls.