in reply to Re^2: A Quick OO Module for parsing lines from an IRC connection
in thread A Quick OO Module for parsing lines from an IRC connection

Just a litte fun:

Ok, it needs a lot more, p.e. you could code the reading part in a thread to loose the blocking.

Irc.pm
package Irc; use strict; use warnings; use Carp; use IO::Socket; sub new { my $class = shift; my $server = shift; my $self = { SERVER => $server, MYSOCKET => undef, }; my $closure = sub { my $field = shift; if (@_) { $self->{$field} = shift; } return $self->{$field}; }; bless ($closure, $class); return $closure; } # a public getter for the SERVER variable sub server { &{ $_[0] }("SERVER") } # a private getter/setter for the SOCKET variable sub socket { caller(0) eq __PACKAGE__ || confess "socket is a private method"; &{ $_[0] }( "MYSOCKET", @_[1..$#_] ); } # log on sub connect { my $self = shift; my $socket = IO::Socket::INET->new( $self->server() ); $self->socket($socket); } # logoff sub disconnect { my $self = shift; my $sock = $self->socket; print $sock "exit\r"; close $sock; } # send (and automatically append \r) sub send { my $self = shift; my $text = shift; my $sock = $self->socket; print $text . "\n"; print $sock $text . "\r"; } # receive something from the server sub read { my $self = shift; my $sock = $self->socket; PINGPONG: while (<$sock>) { print "$_"; # break after receiving PING if (/^PING :(.+)\r$/) { $self->send("PONG :$1"); last PINGPONG; } # break after receiving MOTD if (/ 376/) { last PINGPONG; } } } 1;
irc.pl
#!/usr/bin/perl use strict; use warnings; use Irc; my $session = Irc->new("localhost:6667"); $session->connect(); $session->send("nick whatever"); $session->read(); $session->send("user whatever ignored v1.0 :perl hack"); $session->read(); $session->send("join #room1"); # now keep on reading until receiving the special logoff code ;) $session->read(); $session->disconnect();

Replies are listed 'Best First'.
Re^4: A Quick OO Module for parsing lines from an IRC connection
by SoupNazi (Acolyte) on Aug 28, 2005 at 04:52 UTC

    I am sorry I didn't have a chance to get back to this sooner. That is amazing.

    I am going to noodle this tonight, and tomorrow.

    You seem to have solved a problem I had, ostensibly, how does one make an 'object' out of an irc bot. I was working on a 'personality' which was for this project innappropriate. You made an 'entity', which honestly, I like better. Why have just a method to parse lines when methods could also be created to handle the other aspects of the bot, connecting to the server, etc? Very nice indeed.

    Thank you, sincerely for your input.

    Oh, I was also working on closures, your addition of one helped me to learn a thing or two about that. Thank you for that as well :)

    Regards
    SoupNazi

    Visit us at irc.crazylogic.org #apartment411
    D A R K E M P I R E s u b n e t w o r k