threads not executing @ same time



Hi,

Im new to threading with perl. Im writing an irc bot to fetch rss
headers and post new releases from sites like thepiratebay.org

What happens is when some1 types one of the triggers !help,
!list_filters, !info it creates the thread no probs, but it seems to
work just like a sub.
Ie if another person types another trigger they wont see any info until
the 1st thread finishes.

What I would like to happen is for each thread to start when its
called, any information will be greatly appreciated.

Code below;

# create irc connection in thread

my $thread = threads->create('irc_connect');
$thread->join();

sub irc_connect
{
print "\n### Connecting to irc server: $server\n\n";

our $sock = new IO::Socket::INET
(
PeerAddr => $server,
PeerPort => 6667,
Proto => 'tcp'
) or die "!!! Error Couldnt connect to $server\n";

# Send login informatio

print $sock "NICK $nick\r\n";
print $sock "USER $login 8 * :argghhh\r\n";


# wait for login to be confirmed

while ($main::input = <$sock>)
{
if ($main::input =~ /004/)
{
# 004 = login confirmed
last;
}
elsif ($main::input =~ /433/)
{
die "Nickname is already in use.";
}
elsif ($main::input =~ /^PING \:(.*)$/i)
{
# We must respond to PINGs to avoid being disconnected.
print $sock "PONG $1\r\n";
print "*** PONG :$1\r\n";
}
}

print "### Connected to $server\n\n";

# Join the channel.

#print $sock "JOIN #glug\r\n";

$i = 0;
for (@channels)
{
$i++;
print "\n### JOIN $_\r\n";
print $sock "JOIN $_\r\n";
$speak_mode{$_} = 1;
$sum_mode{$_} = 1;
$rel_mode{$_} = 1;
$chan_filter{$_} = "$i.filter.txt";
}


if(-f $settings_file)
{
do $settings_file;
}

while ($main::input = <$sock>)
{ # check irc input and reply as needed
chop $main::input;

&main::check_input($main::input);
}
exit;
}

# checks irc input and preforms replys

sub check_input {
my $input = shift;
chomp $input;

# show info

# :JohnSilver!mem@xxxxxxxxxxxxxxxxxxxxx PRIVMSG #glug :!info

elsif($input =~ /^\:(.*?)\!.*? PRIVMSG (\#.*?)\s+\:\!info/)
{
@arr = ();
$user = lc $1;

for $chan(@channels)
{
push @arr, "$chan :";
push @arr, "speak mode = ".$speak_mode{$chan};
push @arr, "sum mode = ".$sum_mode{$chan};
push @arr, "rel mode = ".$rel_mode{$chan};
}

&speak_arr_thr($user, @arr);
}
elsif($input =~ /^\:(.*?)\!.*? PRIVMSG (\#.*?)\s+\:\!help/)
{
$user = lc $1;
&speak_arr_thr($user, @main::help);
}

# List filters

# :JohnSilver!mem@xxxxxxxxxxxxxxxxxxxxx PRIVMSG #glug :!list_filters

elsif($input =~ /^\:(.*?)\!.*? PRIVMSG (\#.*?)\s+\:\!list_filters/)
{
$i = 0;
@arr = ();
$user = lc $1;
$chan = lc $2;
@filters = &readf($main::chan_filter{$chan});
for(@filters)
{
$i++;
push @arr, "$i - $_";
}
&speak_arr_thr($user, @arr);
}
}


# speak arr thr

sub speak_arr_thr
{
my ($u, @arr) = @_;

$thread = threads->create(\&speak_arr, $u, @arr);
$thread->join();
# yield();
return;
}

sub speak_arr
{
my ($chan, @arr) = @_;

for my $line(@arr)
{
print $main::sock "PRIVMSG $chan :$line\r\n";
print ">>> $nick \@ $chan | $line\n";
}
}

.



Relevant Pages