T
Ting Wang
I want set envirment variable, e.g PATH
with perl, how can I co it?
Thanks
with perl, how can I co it?
Thanks
Ting said:I want set envirment variable, e.g PATH with perl, how can I co it?
Ting said:I want set envirment variable, e.g PATH
with perl, how can I co it?
Ting Wang said:I want set envirment variable, e.g PATH
with perl, how can I co it?
Ting said:I want set envirment variable, e.g PATH
with perl, how can I co it?
Ting Wang said:I want set envirment variable, e.g PATH
with perl, how can I co it?
J. Romano said:"perldoc -q environment" says that it's almost impossible, but that
there are a few work-arounds, if you are using Unix.
Here's one work-around (for Unix/Linux) that sets your PATH to
"/usr/bin":
#!/usr/bin/perl -w
use strict;
$ENV{PATH} = "/usr/bin";
exec $ENV{SHELL};
__END__
Jürgen Exner wrote:
I guess "It works" by some definition of works.
#!/usr/bin/perl -w
use strict;
$ENV{PATH} = "/usr/bin";
exec $ENV{SHELL};
__END__
All I did was set the PATH by setting $ENV{PATH}, and then adding
"exec $ENV{SHELL}" as the last line of the Perl script. That way,
your environment changes will "stick" when the Perl script ends.
A. Sinan Unur said:(e-mail address removed) (J. Romano) wrote in
...
...
Changes will not stick. You have just invoked a new copy of your shell
with a new environment.
Each time you run this script, a new copy of your shell will run.
Why would you want to do that for something that can be handled much for
easily using your shell's facilities?
A. Sinan Unur said:Changes will not stick. You have just invoked a new copy of your shell
with a new environment.
Each time you run this script, a new copy of your shell will run.
Why would you want to do that for something that can be handled much for
easily using your shell's facilities?
I have to deal with too many "helpful" people who
correct the value of ${PATH}
J. Romano said:You're right: Changes will not stick -- to the old shell. But the
old shell is not around anymore,
and what's left to work with is a new
shell that's practically identical to the old shell but with the
environment changes you made in the Perl script.
It's kind of like a joke I heard once: A robber broke into a house
and stole all the furniture and appliances -- but he replaced them
with exact copies.
The point of the joke is that if the robber replaced everything
with an exact copy, how is that any different (from the rightful
owner's point of view) from the thief not stealing anything at all to
begin with?
In the same way, you might be concerned that a new copy of your
shell was invoked (replacing the old shell), but if the new shell is
almost identical to the old shell, would that really matter?
As it turns out, it does matter in some matter in some cases.
Jürgen Exner pointed out that Perl is often used in batch programming,
in which case the commands after the call to the Perl script would run
in the parent shell and not have the environment changes.
But then,
if the Perl script was invoked with the "exec" command,
the commands
after the call to the Perl script wouldn't get run at all (not to
mention, the Perl script's line "exec $ENV{SHELL}" would open up a new
shell, which is not wanted in batch scripting).
Several times during working on Unix systems (both at the
university and at work) I've had to execute a specific shell script in
order to begin a certain task or run a certain program. This shell
script was provided by the professor or a system administrator, and it
would made changes to my environment ensuring me being able to run the
program needed for me to get my work done. (I would have to execute
this shell script once for each terminal window I had open.)
One problem with these scripts was that they would often add paths
to my $PATH environment variable. They would usually add the
"/usr/bin" path as well as a few X11 paths, even though I already had
them in my $PATH. And often these scripts would call other scripts
(which sometimes would call more scripts) that other professors or
system administrators wrote, and each one of these scripts would add
new entires to my $PATH. So after I ran the one script my professor
(or system administrator) required us to run, my $PATH would have
dozens of duplicate paths.
[Perl script to remove duplicate entries in PATH]This is where the following script comes in handy:
$ENV{PATH} = join ':', @pathList;
exec $ENV{SHELL};
Now I can run:
exec syimplifyPath.pl
You're right: Changes will not stick -- to the old shell. But the
old shell is not around anymore, and what's left to work with is a new
shell that's practically identical to the old shell but with the
environment changes you made in the Perl script.
It's kind of like a joke I heard once: A robber broke into a house
and stole all the furniture and appliances -- but he replaced them
with exact copies.
The point of the joke is that if the robber replaced everything
with an exact copy, how is that any different (from the rightful
owner's point of view) from the thief not stealing anything at all to
begin with?
In the same way, you might be concerned that a new copy of your
shell was invoked (replacing the old shell), but if the new shell is
almost identical to the old shell, would that really matter?
(I assume you meant "... much *more* easily ...")
I'll give you an example:
Several times during working on Unix systems (both at the
university and at work) I've had to execute a specific shell script in
order to begin a certain task or run a certain program.
....
One problem with these scripts was that they would often add paths
to my $PATH environment variable. ....
So after I ran the one script my professor (or system administrator)
required us to run, my $PATH would have dozens of duplicate paths.
A. Sinan Unur said:The solution to that is to start a new shell, run the scripts and do
whatever your professor asks of you etc, then exit that shell.
Jürgen Exner said:Ok, glad to see that you do understand the shortcomings of your approach.
Why would anyone do that? Typically you want a process to call the Perl
script to do some task and then call the next script or program to do the
next task and so on. If you call the sub-scripts using exec() then I
certainly don't want to manage the control flow of that system.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.