Sandy has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,

I would like to run a small perl program that modifies the environment variables, that do not disappear when the program exits. In other words, the perl can't execute as a child process.

In tcsh, one can 'source' another tcsh script. Is there similar kind of thing for executing perl?

PS: I'm working on solaris.

Sandy

UPDATE: I'm using tcsh

  • Comment on Keeping environment variables after perl has exited

Replies are listed 'Best First'.
•Re: Keeping environment variables after perl has exited
by merlyn (Sage) on Feb 19, 2004 at 16:08 UTC
    If you know what kind of shell you are speaking to, you can simply print the right commands to STDOUT, as:
    $ eval `myscript`
    and for sh-style shells, you send out:
    FOO=one; export FOO BAR=two; export BAR
    The problem is you have to be aware about the shell. This is similar to what tset(1) does: see the docs there.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Keeping environment variables after perl has exited
by Abigail-II (Bishop) on Feb 19, 2004 at 16:27 UTC
    That's a FAQ. perldoc -q environment.

    Abigail

Re: Keeping environment variables after perl has exited
by Old_Gray_Bear (Bishop) on Feb 19, 2004 at 16:12 UTC
    As is often the case, the answer is "it depends". All of the shells (or at least all that I have used) provide a method for 'source'ing a script -- run in the current environment, do not spiel off a separate process; 'source', '.', 'run xxx opt=nosep ', etc. It depends on the shell you are using, not the language.

    ----
    I Go Back to Sleep, Now.

    OGB

      sourcing a script means "run this script in the current shell". Which, unless your shell knows how to parse and execute Perl, isn't going to work for a Perl program:
      $ tcsh $ echo 'print "Hello, world\n"' > ppp $ source ppp $ print: Command not found.
      Abigail
        Yep, that's what I thought. I was just hoping for a different answer.

        Sandy

Re: Keeping environment variables after perl has exited
by BrowserUk (Patriarch) on Feb 19, 2004 at 16:46 UTC

    Just a thought as I know little or nothing about unix but would having your program set the environment vars and then exec the appropriate shell give you the result you are after?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Timing (and a little luck) are everything!
Re: Keeping environment variables after perl has exited
by bean (Monk) on Feb 19, 2004 at 21:12 UTC
    You might try a using perl shell like psh or zoidberg and then "source" the script.