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

Got a little problem with a script. Basically looking for someone to point me to a reference on how Perl handles shell variables. Sepcifically export (IE - export DISPLAY).

I've tried a lot of combos, but am not able to nail this one down, unfortunately.

I basically run a script which rsh'es, exports display, then runs a program -- export display obviously doesn't work though.

Tx.

Replies are listed 'Best First'.
Re: shell variables with Perl
by Zaxo (Archbishop) on Sep 23, 2003 at 02:54 UTC

    You'll find the environment variables which are exported to the perl process in the %ENV hash. The name of the variable is the key, e.g. $ENV{'PATH'}.

    After Compline,
    Zaxo

      I actually replied to this yesterady but got busy and didn't get past the preview page.

      All good information. The %ENV hash I totally overlooked. I remember reading it a year or so ago in Learning Perl and thinking "I won't need that for a while." I was right, but now that I needed it...

      For the record, a simple...

      my $display = $ARGV[0]; $ENV{'DISPLAY'} = $display . ":0.0";
      ...worked just fine.
Re: shell variables with Perl
by Roger (Parson) on Sep 23, 2003 at 03:19 UTC
    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
      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
Re: shell variables with Perl
by tachyon (Chancellor) on Sep 23, 2003 at 04:21 UTC
    #!/usr/bin/perl print "\$ENV{$_} = $ENV{$_}\n" for keys %ENV; __DATA__ # prints.... $ENV{SYSTEMROOT} = C:\WINNT $ENV{PROCESSOR_ARCHITECTURE} = x86 $ENV{PROCESSOR_REVISION} = 0602 $ENV{INCLUDE} = C:\Program Files\Microsoft Visual Studio .NET\Framewor +kSDK\include\ $ENV{NUMBER_OF_PROCESSORS} = 2 $ENV{OS} = Windows_NT $ENV{VIMRUNTIME} = d:\vim\vim61 [snip]

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: shell variables with Perl
by Abigail-II (Bishop) on Sep 23, 2003 at 08:10 UTC
    Ah, the famous "it doesn't work". Why do you say that it doesn't work? Did it call in sick? Is it on strike? Did you feed it in the morning? Did you clean its stables?

    Without telling us what you did, what you expected it would do, and what it eventually did, we can only guess. I can imagine a couple of scenarios where it would fail - I can also imagine a couple of scenarious where it wouldn't fail. Details, details, details.

    Abigail