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

Hi, I use both Parallel::ForkManager and File::Where in a large project. For some reason, it seems that where_pm can only be called from the first child process, and this also seems system dependent.

An example to illustrate the problem:

#!perl use Parallel::ForkManager; use File::Where qw(where_pm); use strict; use warnings; my $pm = new Parallel::ForkManager(2); for my $i (1 .. 3) { my $pid = $pm->start and next; my @ret = File::Where->where_pm("File::Where"); print $ret[1]."\n"; $pm->finish; } $pm->wait_all_children;

And, example output:
[pheller@lunatic:~/Source/test]% perl ./test.pl /Library/Perl/5.8.6 Undefined subroutine File::Where::where_pm at ./test.pl line 12 Undefined subroutine File::Where::where_pm at ./test.pl line 12
Any thoughts?

Replies are listed 'Best First'.
Re: File::Where->where_pm failure in forked children?
by ikegami (Patriarch) on Mar 15, 2007 at 01:19 UTC

    File::Where relies on DATA. I bet the file pointer is shared among the child processes.

    Sorry, I must run, so I can't verify my guess or come up with a solution right now. But hopefully, it will put you on the right path.

      Good call. I created a local copy of Where.pm, removed the SelfLoader bits and the example code runs without fail.

      Any ideas how to solve this, short of creating a modified version of File::Where?
        Ok, I figured out a solution myself :-)
        #!perl use Parallel::ForkManager; use strict; use warnings; my $pm = new Parallel::ForkManager(2); for my $i (1 .. 3) { my $pid = $pm->start and next; my @ret = (); my $code = "use File::Where; \@ret = File::Where->where_pm(\"File::W +here\");"; eval $code; print $ret[0]."\n"; $pm->finish; } $pm->wait_all_children;