http://qs321.pair.com?node_id=738160


in reply to Can I do this ???

You can do this with a little cooperation between different processes. Consider the following:

#! /bin/sh perl prog1 -x ./env.sh foo bar [ -f ./env.sh ] && ./env.sh perl prog2 quux

The idea is that prog1 writes valid shell commands to the env.sh file, does whatever else it has to do and then exits. Execution flow returns to the outer shell, which then sources the file that it wrote.

The prog2 program then runs, and picks up what ever environment changes were made by env.sh.

This is rather fragile and insecure. If you are brave and insist on this approach you really should consider using IPC (Inter-Process Communication) for a more robust solution. The idea is to create a channel of communication (child writes, parent reads) between the two. See perlipc for more information.

• another intruder with the mooring in the heart of the Perl

Replies are listed 'Best First'.
Re^2: Can I do this ???
by cmv (Chaplain) on Jan 22, 2009 at 18:09 UTC
    grinder++ I agree with you, that parents cooperating with children can cause a change in the parents environment. Hey, that probably holds true in real life as well...

    My contribution here is that I implemented something similar, using unix signals. The parent would trap on an unused signal (I used SIGUSR1) and read-in-and-execute all the commands contained in a pre-defined temporary file.

    When the child wanted to change something in the parents environment, it would write those changes to the pre-defined temporary file, then would poke-the-parent-in-the-side by sending a signal via the unix kill(1) command.

    This worked well for me.

    -Craig