[ale] [OT] export inside perl

Danny Cox danscox at mindspring.com
Wed Oct 23 10:08:25 EDT 2002


All,

On Wed, 2002-10-23 at 09:44, Fletch wrote:
> 
> $ perldoc -q envrionment
> 
> Found in /usr/lib/perl5/5.6.1/pod/perlfaq8.pod
>        I {changed directory, modified my environment} in a perl
>        script.  How come the change disappeared when I exited the
>        script?  How do I get my changes to be visible?
> 
>                Unix
>                    In the strictest sense, it can't be done--the
>                    script executes as a different process from
>                    the shell it was started from.

	That said, and you still want to do it, you can, but it takes a few
steps.  First, you have to present what you wish to do in the form of
shell commands.  Then you have to get your current shell to source
them.  In perl, you can print the commands to stdout.  So you produce
for example:

cd /some/long/path/to/your/data
export NAME=foo

That's step 1.  When it's all looking good, you need to source it or
eval it.  '.' expects a filename to read, and will search PATH just like
executing a program.  eval is usually what I use.  If your perl script
is named 'bar', then you can enter:

eval "$(bar)"

and the shell will execute the commands produced by 'bar'.  Finally, put
that statement into a shell function in your .bashrc or .bash_profile:

rbar() {
	eval "$(bar)"
}

Now, when you wish to invoke it, just type 'rbar'.  Of course, you don't
have to name it differently.  You could name the function 'bar', and
invoke the full path of the script: $HOME/bin/bar, or something like
that.  It's just that I'm a little leery of naming things the same, as
an infinite invocation recursion can occur, and run you out of process
slots, memory, and kill off other processes (which drives GNOME & X
crazy).  This is the Voice Of Experience.... ;-)

-- 
kernel, n.: A part of an operating system that preserves the
medieval traditions of sorcery and black art.

Danny


---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be 
sent to listmaster at ale dot org.






More information about the Ale mailing list