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

Esteemed Monks, Could you please give me a solution for my below query. Iam setting few ENV variables inside a perl script. How do i make the value of these ENV variables to be persisted outside the perl environment. Thanks in advance for your help.

Replies are listed 'Best First'.
Re: ENV INFORMATION
by Roger (Parson) on May 21, 2005 at 12:42 UTC
    The following technique is what I use from time to time to set environment variables using Perl. The shell I am using is bash.

    #!/usr/bin/perl -w # .setenv.pl use strict; ... ... my $var1 = 'a'; my $var2 = 'b'; ... ... print <<EOL; export VAR1=$var1 export VAR2=$var2 EOL exit(0);
    The .setenv.pl script is called from a shell script, say, .setenv.sh.

    #!/usr/bin/bash if [ -x ~/.setenv.pl ] then eval `.setenv.pl` fi


    Then you source the shell script with the current shell by:

    . ~/.setenv.sh


    This will set the environment variables VAR1 and VAR2 permanently in the current shell.

Re: ENV INFORMATION
by phaylon (Curate) on May 21, 2005 at 11:45 UTC
    I think 'perldoc -q environment' maybe a good start.

    hth,p

    Ordinary morality is for ordinary people. -- Aleister Crowley
Re: ENV INFORMATION
by robartes (Priest) on May 21, 2005 at 12:08 UTC

    I'm afraid you cannot influence your parent's environment from inside the Perl script, and certainly not by setting %ENV keys. Those variables only get propagated 'downwards' in the process tree, towards children of your perl script, not upwards towards the parent.

    As phaylon mentioned, perldoc -q environment describes a hack whereby you output the VAR=value commands from your script and pull that through the shell's eval function, but then you're no longer inside your perl script. And it might not work with all shells either.

    CU
    Robartes-

      I'm afraid you cannot influence your parent's environment from inside the Perl script

      ...unless you happen to be running VMS.

Re: ENV INFORMATION
by TedPride (Priest) on May 21, 2005 at 17:23 UTC
    Or you could store them in a database table, or pass them as hidden form fields.

    EDIT: Given that the Perl-specific ENV variables worth persisting are not usually settable from your Perl script, I assumed that the original poster was asking about persisting CGI ENV variables from Perl. If this was not the case, you can disregard my post.

    Also note that any ENV variables you don't want the viewer to have access to should not be stored as hidden form fields.

      I am still puzzled at how a question that is so obviously about shell scripting could be mistaken for a CGI question.

      When people start talking about passing information around in %ENV, they're obviously talking about shell scripting.

      About the security note, the issue with hidden form fields is not that users can read them, it is that users can set them. For instance take the common flub of putting prices in hidden form fields. Someone can save your page, edit the prices, and strike a special deal. Obviously you want the viewer to see what your prices are. But you don't want your viewer to set them, so you shouldn't use hidden form fields for that.