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

Hi Guys I would like to change a user’s environment from a shell script located in the same directory as I’m running my Perl script from. We usually use shell scripts to do this but would like to use Perl. Within the shell script we usually run the following source /home/<userdir>/bin/set_normal This will change the environment to a normal working environment Is it possible to run the source command within Perl as I can t seemed to invoke the source command? I get the following error message when tying to run my script source /home/<userdir>/bin/set_normal: No such file or directory
#!/usr/bin/perl -w use Cwd; my $dir = getcwd; system( "csh", "source /home/<userdir>/bin/set_normal");

Replies are listed 'Best First'.
Re: Changing a unix users environment
by Corion (Patriarch) on Oct 25, 2007 at 10:47 UTC

    Read system - the list form of system, which you rightfully use, needs every parameter as a single item, not delimited by spaces:

    my $userdir = 'corion'; my $env_file = "/home/$userdir/bin/set_normal"; system("csh", "source", $env_file) == 0 or die "Couldn't source '$env_file': $! / $?";

    But starting a shell which loads the environment into its memory and then exits again won't do what you want anyway, as all changes to the environment are lost once that shell terminates. See tilly's Get default login environment for the technique how to run a shell script and capture all changes to the environment in Perl.

Re: Changing a unix users environment
by derby (Abbot) on Oct 25, 2007 at 11:54 UTC
Re: Changing a unix users environment
by Joost (Canon) on Oct 25, 2007 at 11:54 UTC
    That construct will change the environment for the called shell only. It won't have any effect on the perl program or its parent processes.

    That's also the reason you have to "source" a shell script if you're using it to set environment variables- it's then run by the current shell, in stead of executed by a child process.

Re: Changing a unix users environment
by suaveant (Parson) on Oct 25, 2007 at 13:02 UTC
    if you want to use a Perl script to change your current shell environment you basically have to make a shell script that evals the output of the Perl script, then source the shell script (or run the eval manually).

    Something like:

    #!perl print "setenv FOO bar\n";
    shell script would have...
    eval `perl_script.pl`
    or whatever... is a pain since you still need to output shell commands, and may or may not need to semi-colon separate them...

                    - Ant
                    - Some of my best work - (1 2 3)

      the same
      ~/bdimych >cat x.pl #!/usr/bin/perl print "eval export \"MYENVVAR=MY ENV VALUE3\";\n"; print ": : If you see this, then you launch this script in wrong way : You should launch it only backslashed i.e. `$0` : See \"Command Substitution\" in man bash : "; ~/bdimych >echo $MYENVVAR ~/bdimych >./x.pl eval export "MYENVVAR=MY ENV VALUE3"; : : If you see this, then you launch this script in wrong way : You should launch it only backslashed i.e. `./x.pl` : See "Command Substitution" in man bash : ~/bdimych >`./x.pl` ~/bdimych >echo $MYENVVAR MY ENV VALUE3 ~/bdimych >
Re: Changing a unix users environment
by tuxz0r (Pilgrim) on Oct 25, 2007 at 16:27 UTC
    I posted a snippet a while back that is probably what you're looking for. Just put it at the top of your perl script and reference the 'set_normal' shell script you mentioned above and give it a try.