perlexecchild-processqx

Modules inherited by child process from parent process in Perl


Example of Execution:

execute.pl -> system("test1.pl") -> system("test2.pl")

So the test1.pl and test2.pl should pick up the overridden die/warn/say/print if I just include Logging.pm in excute.pl.

As far I know system/qx/exec will be OS call and Logging.pm won't be available in the child process, is there any way I can achive this as I don't want to edit 300 files?


Solution

  • Since the sub-processes are entirely separate processes they will not keep any modules loaded by the parent process.

    One possibility to solve this is to set the PERL5OPT environment variable. This variable can hold extra command line flags for the Perl interpreter. However, this will affect all Perl processes started directly or indirectly by your script, not just those scripts that are part of your project.

    To automatically use Logging, you'd add -MLogging to the PERL5OPT. In shell:

    $ export PERL5OPT="$PERL5OPT -MLogging"
    $ ./execute.pl
    

    or

    $ PERL5OPT="$PERL5OPT -MLogging" ./execute.pl
    

    or within execute.pl:

    $ENV{PERL5OPT} .= " -MLogging";