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

I am writing a simple IRC client that connects to a handful of servers and waits for input. Current I can correctly join and listen to one server, but I am unsure of how to add multiple servers to the mix. If someone could point me in the right direction I'd appreciate the assistance. I've been messing around with IO::Select but I'm not sure if that's the correct approach or not.
my $sock = new IO::Socket::SSL(PeerAddr => 'myircserver', PeerPort => +'6697', Proto => 'tcp') or die "Can't connect\n"; print $sock "NICK mlapaglia\r\n"; print $sock "USER mlapaglia\r\n"; while(my $input = <$sock>) { if($input =~ /004/) { last; } elsif($input =~ /433/) { die "nickname already in use\n"; } } print $sock "JOIN #testing123\r\n"; while(my $input = <$sock>) { chop $input; if($input =~ /^PING(.*)$/i) { print $sock "PONG $1\r\n"; } elsif($input =~ /^:(.*)!(.*)\@(.*?) PRIVMSG (.*)/i) { event_privmsg("myserver", $4, $1, $3); } }

Replies are listed 'Best First'.
Re: Perl Simple IRC Client question
by Corion (Patriarch) on Feb 18, 2010 at 20:03 UTC
      I'd like to stick to not having to require modules if it's possible. I got IO::Select working properly, and I can now listen to multiple servers. I have another question though. I'm eventually going to have a large handful of servers, and hard coding each one seems a bit sloppy. I'm trying to use the following code to clean it up a bit:
      my $sock = new IO::Socket::SSL(PeerAddr => 'irc.server2.org', PeerPort + => '6697', Proto => 'tcp') or die "Can't connect\n"; my $sock2 = new IO::Socket::SSL(PeerAddr => 'irc.server1.org', PeerPor +t => '6697', Proto => 'tcp') or die "Can't connect\n"; my %Servers = (server1 => $sock, server2 => $sock2); foreach my $out (keys %Servers) { print $Servers{$out} "NICK mlapaglia\r\n"; print $Servers{$out} "USER mlapaglia\r\n"; }
      but it's failing at print $Servers{$out} "NICK mlapaglia\r\n"; When I try print $sock "NICK mlapaglia\r\n"; it works properly. Am I dereferencing incorrectly?

        You should really use some of the IRC modules instead of writing this yourself. For example if the remote server is very slow (or throttles you), your attempt of blockingly printing a whole line in one go will stall your whole client even if it's just one server that is slow.

        But anyway, see print about how to print to any expression more complex than a simple scalar.

Re: Perl Simple IRC Client question
by rowdog (Curate) on Feb 19, 2010 at 06:27 UTC