View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Jeremy Muhlich > Net-IRC-0.75 > Net::IRC



Annotate this POD (1)

Related Modules



New  5
Open  1
View/Report Bugs
Module Version: 0.75   Source   Latest Release: Net-IRC-0.79


Net::IRC - Perl interface to the Internet Relay Chat protocol


    use Net::IRC;

    $irc = new Net::IRC;
    $conn = $irc->newconn(Nick    => 'some_nick',
                          Server  => '',
                          Port    =>  6667,
                          Ircname => 'Some witty comment.');


Welcome to Net::IRC, a work in progress. First intended to be a quick tool for writing an IRC script in Perl, Net::IRC has grown into a comprehensive Perl implementation of the IRC protocol (RFC 1459), developed by several members of the EFnet IRC channel #perl, and maintained in channel #net-irc.

There are 4 component modules which make up Net::IRC:

The central concept that Net::IRC is built around is that of handlers (or hooks, or callbacks, or whatever the heck you feel like calling them). We tried to make it a completely event-driven model, a la Tk -- for every conceivable type of event that your client might see on IRC, you can give your program a custom subroutine to call. But wait, there's more! There are 3 levels of handler precedence:

And even better, you can choose to call your custom handlers before or after the default handlers instead of replacing them, if you wish. In short, it's not perfect, but it's about as good as you can get and still be documentable, given the sometimes horrendous complexity of the IRC protocol.



To start a Net::IRC script, you need two things: a Net::IRC object, and a Net::IRC::Connection object. The Connection object does the dirty work of connecting to the server; the IRC object handles the input and output for it. To that end, say something like this:

    use Net::IRC;

    $irc = new Net::IRC;

    $conn = $irc->newconn(Nick    => 'some_nick',
                          Server  => '');

...or something similar. Acceptable parameters to newconn() are:


Once that's over and done with, you need to set up some handlers if you want your bot to do anything more than sit on a connection and waste resources. Handlers are references to subroutines which get called when a specific event occurs. Here's a sample handler sub:

    # What to do when the bot successfully connects.
    sub on_connect {
        my $self = shift;

        print "Joining";
        $self->privmsg("", "Hi there.");

The arguments to a handler function are always the same:


The Connection object that's calling it.


An Event object (see below) that describes what the handler is responding to.

Got it? If not, see the examples in the irctest script that came with this distribution. Anyhow, once you've defined your handler subroutines, you need to add them to the list of handlers as either a global handler (affects all Connection objects) or a local handler (affects only a single Connection). To do so, say something along these lines:

    $self->add_global_handler('376', \&on_connect);     # global
    $self->add_handler('msg', \&on_msg);                # local

376, incidentally, is the server number for "end of MOTD", which is an event that the server sends to you after you're connected. See for a list of all possible numeric codes. The 'msg' event gets called whenever someone else on IRC sends your client a private message. For a big list of possible events, see the Event List section in the documentation for Net::IRC::Event.

Getting Connected

When you've set up all your handlers, the following command will put your program in an infinite loop, grabbing input from all open connections and passing it off to the proper handlers:


Note that new connections can be added and old ones dropped from within your handlers even after you call this. Just don't expect any code below the call to start() to ever get executed.

If you're tying Net::IRC into another event-based module, such as perl/Tk, there's a nifty do_one_loop() method provided for your convenience. Calling $irc->do_one_loop() runs through the event loop once, hands all ready filehandles over to the appropriate handler subs, then returns control to your program.


This section contains only the methods in itself. Lists of the methods in Net::IRC::Connection, Net::IRC::Event, or Net::IRC::DCC are in their respective modules' documentation; just perldoc Net::IRC::Connection (or Event or DCC or whatever) to read them. Functions take no arguments unless otherwise specified in their description.

By the way, expect Net::IRC to use AutoLoader sometime in the future, once it becomes a little more stable.



Up-to-date source and information about the Net::IRC project can be found at .


syntax highlighting: