Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > how perl set envirment variable

Reply
Thread Tools

how perl set envirment variable

 
 
Ting Wang
Guest
Posts: n/a
 
      07-10-2004
I want set envirment variable, e.g PATH
with perl, how can I co it?

Thanks
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-10-2004
Ting Wang wrote:
> I want set envirment variable, e.g PATH with perl, how can I co it?


Read about the %ENV variable in "perldoc perlvar".

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      07-10-2004
Ting Wang wrote:

> I want set envirment variable, e.g PATH
> with perl, how can I co it?


That's a FAQ.
perldoc -q environment

Note: Win32 acts different than Unix/Linux in regards to this.
-Joe
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      07-10-2004
Ting Wang <(E-Mail Removed)> wrote:

> I want set envirment variable, e.g PATH
> with perl, how can I co it?



$ENV{PATH} = '/bin';


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      07-10-2004
Ting Wang wrote:
> I want set envirment variable, e.g PATH
> with perl, how can I co it?


Depends on what you want to achive. Please check
- "perldoc perlvar", section about %ENV
- "perldoc -q env"

jue


 
Reply With Quote
 
J. Romano
Guest
Posts: n/a
 
      07-10-2004
Ting Wang <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)-stuttgart.de>...
> I want set envirment variable, e.g PATH
> with perl, how can I co it?



Dear Ting Wang,

"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__


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.

What is really happening is that a child process was made (that's
why the environment changes stick) so, for every time you run this
Perl script, you have to type an extra "exit" at the prompt when you
want to leave, unless you run the script with the UNIX "exec" command,
like this:

exec script.pl

Keep in mind that these environment changes aren't permanent. Once
you logout or exit your shell you will have to run your script again
to get your changes back.

I hope this helps,

Jean-Luc
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      07-10-2004
J. Romano wrote:
> Ting Wang <(E-Mail Removed)> wrote in message
> news:<(E-Mail Removed)-stuttgart.de>...
>> I want set envirment variable, e.g PATH
>> with perl, how can I co it?

>
> "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__


I guess "It works" by some definition of works.
However your approach provides a workaround for only one user scenario, and
a rare scenario for that matter. How often do you really want to open a new
shell? Typically people would rather continue in the given shell.
Not to mention that your approach is utterly useless in batch programming
which is one of the main application areas of Perl.

jue


 
Reply With Quote
 
Derek Ludwig
Guest
Posts: n/a
 
      07-10-2004

>> Ting Wang <(E-Mail Removed)> wrote in message
>> I want set envirment variable, e.g PATH
>> with perl, how can I co it?


>> J. Romano wrote:
>> 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.

<SNIP>
> jue


Actually, this shows that you can you can set $ENV{PATH},
and effect subsequent calls to exec, system, etc. If have
had path problems in a batch environment, so I ended up
with the (somewhat cheesy):

use local::Batch::Env;

to set my local environment, including $ENV{PATH}, so
that system does work they way I want. I have to deal
with too many "helpful" people who correct the value
of ${PATH} :-/.

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-10-2004
(E-Mail Removed) (J. Romano) wrote in
news:(E-Mail Removed) om:

....

> #!/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.


....

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
(E-Mail Removed) (reverse each component for email address)
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      07-10-2004
A. Sinan Unur <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> (E-Mail Removed) (J. Romano) wrote in
> news:(E-Mail Removed) om:
>
> ...
>
> > #!/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.

>
> ...
>
> 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?


One of those is the eval function that most shells have. You can say

eval `perl_script`

in your shell and pull in the power of Perl, if needed.

The perl_script must print a bit of shell code which "eval" will
execute, say "PATH = something; export PATH" for a bourne-like shell.
The eval command can also be conserved in an alias for interactive
use.

That is a much better approach than heavy-handedly starting another shell.

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
Object variable or With Block variable not set error =?Utf-8?B?TmV3YmllRGV2?= ASP .Net 0 04-26-2006 03:11 AM
Object variable or With block variable not set Neo Geshel ASP .Net 6 03-23-2006 06:50 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments