in reply to Setting Environment variables in Perl

It works for me:

$ echo $FOO $ cat >foo.sh #!/bin/sh echo $FOO $ chmod 755 foo.sh $ cat >foo.pl #!/usr/bin/perl $ENV{'FOO'} = 'bar'; system './foo.sh'; $ perl foo.pl # corrected, Hue-Bond++ bar $
What does your code look like?

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Setting Environment variables in Perl
by Hue-Bond (Priest) on Sep 12, 2005 at 14:14 UTC

    Just a little note:

    $ cat >foo.pl #!/usr/bin/perl $ENV{'FOO'} = 'bar'; system './foo.sh'; $ perl foo.sh

    I think you mean perl foo.pl.

    --
    David Serrano

Re^2: Setting Environment variables in Perl
by gargle (Chaplain) on Sep 12, 2005 at 07:55 UTC

    Hi,

    It works because $ENV gives you access to your current environment and child environments. Once your foo.pl ends your FOO will be gone.

    Try executing foo.sh once more after foo.pl finishes. You'll see the value of FOO being empty.

    --
    if ( 1 ) { $postman->ring() for (1..2); }

      The OP said:

      I have a situation where i have to set shell environment variables within perl script and invoke shell command which uses this environment variable using system() function.

      So the commands are being run from within the Perl script using system(). They will be children of the Perl and will inherit it's environment.

      Cheers,
      R.

      Pereant, qui ante nos nostra dixerunt!
      Yes, a process can never change the environment variables of a parent process, which is what would happen if FOO was still there when the script finished (a perl script runs in a child process from your shell). /Hacker