PERL IRC Bot Skeleton

  • Author:xnite
  • Date:2015/07/07

More recently than ever people have been approaching me for help with learning a programming language. More often than not I will refer them to PERL or PHP depending on their program requirements. Surprisingly the best technique, that I have found, to help someone learn a new language is through writing an IRC bot. This requires someone to learn about not only the PERL basics, such as setting variables and working with scopes, but also pushes them to dive into other stuff like working with sockets and regular expressions. I’ve found that with a short primer from the getting started section of PerlMonks` tutorials, and simply learning to understand the documentation for various libraries listed on CPAN, all of the people whom I’ve introduced PERL to have been able to pick it up rather easily.

I’ve provided this PERL IRC bot skeleton to a few people and you are completely free to use it as well. Whether you are just beginning to learn PERL, or you are simply interested in writing an IRC bot with PERL, this example skeleton is a great starting point offering only the basics of connecting to a server and playing PING-PONG with the server. The rest is up to you. 🙂 Feel free to comment or email me for a few pointers if you need.

#!/usr/bin/perl
use strict;
use IO::Socket::INET;

my $nick	= 'Person';
my $ident	= 'person';
my $name	= 'Person McPeople';

my $sock = new IO::Socket::INET(
	PeerAddr	=> 'irc.buddy.im',
	PeerPort	=> 6667,
	Proto		=> 'tcp',
	Timeout		=> 15 ) or exit;

## Establish connection to IRC server by entering nickname, and user information.
print $sock "NICK ".$nick."\r\n";
print $sock "USER ".$ident." ".$ident." 0 :".$name."\r\n";

while( my $buffer = <$sock> )
{
	if( $buffer =~ /^PING(.*)$/i )
	{
		print $sock "PONG ".$1."\r\n";
	} elsif( $buffer =~ /^(.*?) 376 (.*) :(.*)/i )
	{
		## Received end of MOTD, join a channel and say hi.
		print $sock "JOIN #Lobby\r\n";
		print $sock "PRIVMSG #Lobby :Hello everybody!\r\n";
	} else {
		print $buffer;
	}
}
exit;
Robert Whitney
I'm a geek, gamer and breaker of things. I'm a programmer by day and an apache attack helicopter by night. Some would call me their spirit animal.
Opinions expressed here, even 💩 ones, are my own and do not represent those of my employer or associates.
Referral Links

Using my referral links is the best way to help me pay for my projects and development servers and get something out of it for yourself.

Copyright©2011 - 2018, Robert Whitney; All rights reserved.