Modules inherited by child process from parent process in Perl

252 Views Asked by At
  • I am integrating a logging framework to my perl project which has around 300 Perl files.
  • So I have written a module Logging.pm which has overridden die , say , warn functionalities and since print cannot be overridden I have tied it my custom handle.
  • I have a master script execute.pl which executes all the scripts through system/qx/exec.
  • I want to include Logging.pm in just execute.pl and all the functionalities of Logging.pm should be availabe in the child process executed by execute.pl through system() or qx() or exec().

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?

1

There are 1 best solutions below

0
On BEST ANSWER

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";