in reply to system ( "source $script" )

I'd like to implement an option where the user could see what the result of sourcing all of the init files would be.

Maybe something like this:

system ". $script && env | sort";

This would run a new shell, source the $script, and print the resulting environment. As it's a new shell, it wouldn't affect the parent shell (as desired).

(If you want a shell other than what 'perl -V:sh' reports (i.e. what Perl is using by default), you have to run it explicitly, e.g. system "/bin/tcsh", "-c", "shell code here...";)

Replies are listed 'Best First'.
Re^2: system ( "source $script" )
by ajwood (Novice) on Jan 28, 2011 at 21:24 UTC

    Hold on, that doesn't seem to work. The environment as seen by the perl script is unaffected.

    setter.sh:

    #!/bin/sh
    export VAR=SET
    

    script.pl:

    #!/usr/bin/perl
    system ( ". ./setter.sh" );
    print "VAR=$ENV{VAR}\n";
    

    This gives me VAR= and was hoping for VAR=SET

      This is not what I said.

      You need to print the environment from within the new shell started by system() — e.g. with env, as shown.  (But nothing keeps you from using Perl code to print out the environment, in case you don't like env's output:  system ". $script && ./my_env_prettyprinter.pl" )

      As soon as system() returns, the shell has terminated, and the new environment is gone with it. You cannot have a subshell set environment variables in a parent process, such as your Perl script, or the shell the Perl script has been started from, etc.

        Sorry, I misunderstood. Does perl have something analogous to the shell's source? I'd like to apply the initialization scripts to the shell which the Perl script is running in.