in reply to shell variables with Perl

Case 1 - If you want to change shell variable and then execute another program within a single Perl script:
$ENV{DISPLAY} = "10.16.119.3:0.0"; `another_program`;
Case 2 - If you have a shell script that calls a Perl script to permanently set environment variable, and then run another program:

Well, you can't. As soon as your perl script exits, the original shell variables are restored by the shell (well, more strictly speaking, depending on the implementation of the underlying operating system, the shell script interpreter forks and then executes the perl script, the perl script has only modified a local copy of the environment variables in the child process.) However this doesn't mean this is impossible. The trick is to let Perl print the following line to STDOUT (assume that you are using bash):
print "export DISPLAY=$display_value\n";
And then in your shell script, you do this:
#!/usr/bin/bash # Create a temporary shell script using perl perl_program.pl > /tmp/setenv.$$ # source the temporary script and then remove it . /tmp/setenv.$$ rm -f /tmp/setenv.$$ # run another program with the modified environment another_program

Replies are listed 'Best First'.
Re: Re: shell variables with Perl
by iburrell (Chaplain) on Sep 23, 2003 at 17:25 UTC
    If he is going to use bash specific shell script, he can use the bash eval command to eliminate the need for a temporary file.
    $!/usr/bin/bash eval $(perl_program.pl) another_program