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

Hi, I am trying to execute a sh script from a perl script. I am doing that by including the following line to the perl script `. nameofshscript arg1toshscript arg2roshscript`; The sh script modifies some env. variables. however when the execution returns to the perl script after the sh script is done those modification aren't visible in the perl script. what am i doing wrong and how to correct that ? Thanks.. Amit

Replies are listed 'Best First'.
Re: running sh script from perl script
by ehdonhon (Curate) on Jul 17, 2002 at 20:25 UTC

    This is what is happening:

    • Perl forks itself. The main program then wait()'s for the child to finish.
    • The child gets a copy of everything that is in the parents environment (actually, it shares the memory using copy-on-write, but thats more detail than we need right now).
    • When the child goes away, everything that was in the child's copy is destroyed.
    • When the parent resumes operation, it has the same environment as before it forked since nothing ever actually changed it's copy.

    If you need to have a second process alter the memory or environment of another process, you need to look into programming with threads or shared memory. Your other alternative would be inter-process communication.

Re: running sh script from perl script
by DamnDirtyApe (Curate) on Jul 17, 2002 at 21:56 UTC
Re: running sh script from perl script
by arturo (Vicar) on Jul 17, 2002 at 20:23 UTC

    The backticks execute the command in a subshell; so you're not changing the environment of the script that calls that subshell (i.e. your perl script). So you're not really doing anything wrong, per se. That's a limitation of the shell, not Perl. If you want to modify the environment your perl script is running under, do it by modifiying the %ENV hash from within your script.

    I mistrust all systematizers and avoid them. The will to a system shows a lack of integrity -- F. Nietzsche

Re: running sh script from perl script
by robobunny (Friar) on Jul 17, 2002 at 20:23 UTC
    it is only modifying the environment of the subshell that is running your shell script. the syntax "." only works in sh/bash/etc. all it is doing is saying "run these commands as if i typed them at the command prompt". if you want to modify environment variables inside your perl script, you will need to modify $ENV yourself.
Re: running sh script from perl script
by coreolyn (Parson) on Jul 17, 2002 at 20:22 UTC

    When referencing any code in your questions you should utilize <CODE> tags.

    Now that thats out of the way, just dump using the sh script and update your variables via the %ENV hash as in $ENV{'JAVA_HOME'} etc.. However if sourcing existing sh files is mandantory, read the source file via your Perl script and regex for the name=value pairs and update the %ENV based on your results.

      Thanks...i found the porblem in my perl script....i was using
      $ENV{$key} = chomp($value};
      Whicc is wrong. i should have used
      chomp($value); $ENV{$key} = $value;
      thanks to tye....he/she saved the day..... Amit (amit_ra)