How do I fork correctly in a perl module for znc?

Posted by rarbox on Stack Overflow See other posts from Stack Overflow or by rarbox
Published on 2010-05-13T19:06:12Z Indexed on 2010/05/14 1:54 UTC
Read the original article Hit count: 299

Filed under:
|
|

I'm currently writing an IRC bot. The scripts are loaded as perl modules in ZNC but the bot gets disconnected with an Input/Output error if I create a forked process. This is a working example script without fork, but this causes the bot to freeze until the script finishes doing its task.

package imdb;

use warnings;
use strict;


sub new
{
 my ($class) = @_;
 my $self = {};

 bless( $self, $class );
 return( $self );
}

sub OnChanMsg 
{
 my ($self, $nick, $channel,$text) = @_;

#unless (my $pid = fork()) {

 my $result = a_slow_process($text); 
 ZNC::PutIRC( "PRIVMSG $channel :$result" );
# exit;
#}

 return( ZNC::CONTINUE );

}

sub OnShutdown
{
 my ( $me ) = @_;
}

sub a_slow_process { 
my $input = shift; 
sleep 10; 
return "You said $input.";
 }

1;

The fork code that is causing the error is commented out. How do I fix this?

Edited to add: I was told that ZNC::PutIRC should not be put in the child process.

© Stack Overflow or respective owner

Related posts about perl

Related posts about fork