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

Dear Friends! I recently installed the Proc::Fork module from CPAN into a local directory for testing. I then copied and pasted the "Simple Example" from here http://search.cpan.org/~aristotle/Proc-Fork-0.61/lib/Proc/Fork.pm#Simple_example However, when I ran the program, I got an error saying, "Can't locate object method "writer" via package "IO::Pipe::End" at ./simple_fork.pl line 19." I cannot understand why this is happening. All I am trying to do is to run the example on how to spawn child processes. Is there a good tutorial, complete example that I can refer to, to learn how to write a program that spawns child processes to take care of a task and thereafter kills the child process? If this question is a repeat, I apologise! Many thanks to all!!

Replies are listed 'Best First'.
Re: IO:Pipe functioning
by cormanaz (Deacon) on Jun 04, 2008 at 22:36 UTC
    I don't know why you're having this problem. But having tried a lot of the threading modules I found Parallel::ForkManager to be the most straightforward and reliable. Other monks recommended it too. You might give it a try instead.

    Steve

Re: IO:Pipe functioning
by kyle (Abbot) on Jun 05, 2008 at 15:30 UTC

    Correct me if I'm wrong, but the example you're trying to run is this one:

    This example works for me (after I install Proc::Fork).

    The message you're getting (Can't locate object method "writer" via package "IO::Pipe::End") sounds to me as if IO::Pipe hasn't been loaded, but if that were the case, it should fail much earlier with the message: Can't locate object method "new" via package "IO::Pipe" (perhaps you forgot to load "IO::Pipe"?)

    So I'm wondering if you've changed anything.

    In general, when I want to run a child, I just use fork directly. For the kind of problem solved by the example, I use open with special '-|' second argument. For example:

    # This forks implicitly my $child_pid = open my $child_fh, '-|'; if ( ! defined $child_pid ) { die "Can't fork: $!"; } if ( $child_pid ) { # parent print while ( <$child_fh> ); # this implicitly waits for the child close $child_fh or die "close on pipe failed: $!"; } else { # child # child's STDOUT is connected to the pipe print "Line 1\n"; print "Line 2\n"; exit; }