sonalig has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am setting few environment variables in my perl script as:
$ENV{'TEST'}="/nfs/test";
I am able to use this env variable in the rest of the perl scripts. But when I try to do echo $TEST on bash, it doesn't print anything. Is it beacuse the environment variables are getting set in the process that runs the perl script and not in the main shell? Please clarify my doubt. Thanks, Sonali

Replies are listed 'Best First'.
Re: Unable to see environment variable set
by psini (Deacon) on Jul 21, 2008 at 18:31 UTC

    That's it. %ENV contains the environment for your process and for children processes. See perlvar for more details

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

      Actually this question in perlfaq8 might be a more apposite pointer.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

Re: Unable to see environment variable set
by Anonymous Monk on Jul 21, 2008 at 18:39 UTC
    as bash faq puts it A subprocess cannot affect its parent's environment.
Re: Unable to see environment variable set
by leocharre (Priest) on Jul 21, 2008 at 18:41 UTC

    I want to mention a variation on this kind of unexpected- or unintuitive behaviour.

    I remember once writing a script that would work either as a cgi or a cli script.
    At some point what was driving me crazy, was that the thing seemed to respond different under the two environments.
    A couple of things were happening. Albeit owned by 'username' was being run under 'apache' (since the server is making the call, without suexec) and the environment variables even under suexec may be different when run as cgi.

    Another example of environment variable surprises.. This one was much more interesting.. cron. Your root crontab may be running scripts as nobody- or.. your /etc/crontab (if you keep it in a file) may be setting environment variables- like HOME to / instead of /root.

    So, runnig a script via the cli and then doing the same thing out of a crontab, could yield different environments for the script.