Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Set shell environment variables from within a perl script

by reluctant_techie (Novice)
on May 05, 2008 at 16:55 UTC ( [id://684685]=perlquestion: print w/replies, xml ) Need Help??

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

Hello!

I am attempting to set a shell (bash) environment variable from within a Perl script but I have not been able to come up with a satisfactory solution. In particular, I am attempting to set the CVSROOT environment variable so that I can use the "system()" syntax to execute cvs commands. I had hoped the following syntax would work:

system "export CVSROOT=<cvs>"
It does not.

Another solution would be to create a shell script on the fly which would contain a command to set the environment variable. My Perl script would then execute the shell script. This seems pretty messy.

Is there a better solution?

Thanks in advance!

Replies are listed 'Best First'.
Re: Set shell environment variables from within a perl script
by Corion (Patriarch) on May 05, 2008 at 17:01 UTC

    It's not clear to me which environment you want to affect.

    If you want to affect the environment of your process or your child processes, just use the %ENV hash:

    $ENV{CVSROOT}='<cvs>';

    If you want to affect the environment of your parent process, you can't. At least not without cooperation of the parent process. The standard process is to emit a shell script and have the parent process execute that shell script:

    #!/usr/bin/perl -w print 'export CVSROOT=<cvs>';

    ... and call that script from the shell (script) as:

    `myscript.pl`

    eval `myscript.pl`

    system has no effect because it creates a child process, which dutifully changes its environment and then exits. And at process exit, all changes to the environment of that process are lost.

    Update: Fixed shell code as per moritz++ reply

      Actually eval `myscript.pl` Wait, yours works as well, disregard this node :)
Re: Set shell environment variables from within a perl script
by Joost (Canon) on May 05, 2008 at 17:03 UTC
      system("cvs whatever") and die;

      I might be wrong but wouldn't exec("cvs whatever"); have the same effect?

      Cheers,

      JohnGG

Re: Set shell environment variables from within a perl script
by rovf (Priest) on May 05, 2008 at 17:06 UTC
    In the way you were doing it, the environment variable would be gone when the system() call ended (since it was set only in the subshell). You have (at least) two options:

    ENV{CVSROOT}="...";
    This would also change the environment of your perl program itself, and would hence be propagated to all other child processes; most likely, this is what you want to do in the special case of CVSROOT, isn't it?

    The other possibility (assuming that you always shell out to bash, or ksh, or zsh), would go like this: Assume that you want to call an external program myprog with CVSROOT set to a special value, you could use

    system('CVSROOT="...." myprog myarguments');
    This is useful if you want the change of the environment only for that particular call.

    -- 
    Ronald Fischer <ynnor@mm.st>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://684685]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-28 19:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found