J
J. Romano
Dear Perl community,
In the past I have tried to find an answer to the question of how
to set environment variables in Perl scripts and make them last even
after the Perl script has finished. I eventually found the response
mentioned in "perldoc -q environment", which basically says that it
can't be done (although there are work-arounds if you're willing to
use a few Unix tricks).
However, I just recently discovered an easy way to do it. It works
like a charm in Unix. (But unfortunately, it doesn't work so great on
Win32.)
Basically, you write your script as normal, setting environment
variables, changing them, deleting them, and even changing
directories:
$ENV{EDITOR} = "/usr/bin/vi"; # make vi default editor
$ENV{PATH} .= ":/usr/bin/games"; # add games dir to path
delete $ENV{PAGER}; # remove the pager environment variable
chdir($ENV{HOME} . "/bin") or warn $!; # change to my bin dir
But at the end of the script, add this line:
exec $ENV{SHELL};
Run your script, and voila'! The environment variable changes stick!
I've found it's best to run your script with "exec" in front of it,
like so:
exec perl script.pl
otherwise, you'll have to type an extra "exit" for every time you run
the script in order to fully exit the shell.
Of course, this script assumes that your SHELL environment variable
exists and that it's set to your current shell.
In DOS using ActiveState Perl, I've found that changing:
exec $ENV{SHELL};
to:
system 'cmd';
works better than changing it to:
exec 'cmd';
(I have a hunch that ActivePerl implements the exec() call by
replacing it with system() and exit(), but I don't know for sure...)
As far as I know, in DOS you can't run the exec command, so you'll
have to type an extra "exit" to close down the DOS terminal window.
And deleting an evironment variable doesn't seem to work, either (at
least, not when I tried it). No error is generated; it just doesn't
appear to work.
So this approach doesn't fare so well in Win32 DOS, but it looks to
me that it works great in Unix. This goes against what the perldoc
says, so if anybody knows a caveat about using this technique (on
Unix) that I don't see, please speak up. But if there are no
problems, then I would think that this popular dilemma has been
solved.
Happy Perling!
-- Jean-Luc
In the past I have tried to find an answer to the question of how
to set environment variables in Perl scripts and make them last even
after the Perl script has finished. I eventually found the response
mentioned in "perldoc -q environment", which basically says that it
can't be done (although there are work-arounds if you're willing to
use a few Unix tricks).
However, I just recently discovered an easy way to do it. It works
like a charm in Unix. (But unfortunately, it doesn't work so great on
Win32.)
Basically, you write your script as normal, setting environment
variables, changing them, deleting them, and even changing
directories:
$ENV{EDITOR} = "/usr/bin/vi"; # make vi default editor
$ENV{PATH} .= ":/usr/bin/games"; # add games dir to path
delete $ENV{PAGER}; # remove the pager environment variable
chdir($ENV{HOME} . "/bin") or warn $!; # change to my bin dir
But at the end of the script, add this line:
exec $ENV{SHELL};
Run your script, and voila'! The environment variable changes stick!
I've found it's best to run your script with "exec" in front of it,
like so:
exec perl script.pl
otherwise, you'll have to type an extra "exit" for every time you run
the script in order to fully exit the shell.
Of course, this script assumes that your SHELL environment variable
exists and that it's set to your current shell.
In DOS using ActiveState Perl, I've found that changing:
exec $ENV{SHELL};
to:
system 'cmd';
works better than changing it to:
exec 'cmd';
(I have a hunch that ActivePerl implements the exec() call by
replacing it with system() and exit(), but I don't know for sure...)
As far as I know, in DOS you can't run the exec command, so you'll
have to type an extra "exit" to close down the DOS terminal window.
And deleting an evironment variable doesn't seem to work, either (at
least, not when I tried it). No error is generated; it just doesn't
appear to work.
So this approach doesn't fare so well in Win32 DOS, but it looks to
me that it works great in Unix. This goes against what the perldoc
says, so if anybody knows a caveat about using this technique (on
Unix) that I don't see, please speak up. But if there are no
problems, then I would think that this popular dilemma has been
solved.
Happy Perling!
-- Jean-Luc