John Bokma said:
A web based chat uses a method I call infinite loading. Basically the
browser opens the url, but it never stops, like it is downloading an
infinite long page. The chat system just keeps adding new lines.
Is there a way to do this in Perl. I mean, is there a module that let's me
see if there is data available, and read it, and move on in a loop?
I can write it, but I don't like reinventing wheels.
I once wrote a IRC bot using only IO::Socket, that connected to an IRC server,
logged on and executed command when specific strings were sent from the server.
Sounds like all you want to do is connect to a web server on port 80, request a
specific URI and then just read the data the server sends you.
It should be noted that I was a -complete- novice when I wrote that script (as
if THAT has changed significantly), so using IO::Socket might have been a
really bad idea, but it worked.
It looked something like this (bits and parts of the app, with what I remember
to be the things that made it work, this won't work since I snipped it heavily):
------------------------
#!/usr/bin/perl
# no strict or warnings here, suppress them all! Screw the Real Way!
# lots of setup stuff snipped, like $server declarations and such.
$|++; # Don't buffer output
use IO::Socket;
$host = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => "$server",
PeerPort => "$port",
) or { error("Cannot connect to host: $server \n$!") };
$host->autoflush(1);
# This, uh.. flushes every line to output...? I don't remmeber
$SIG{ALRM}='timeout'; # When alarm triggers, run sub 'timeout'.
# If we didn't die, the connection is made
print $host "NICK $nickname\n";
print $host "USER sandman sandman.net $server\n";
alarm 30; # trigger the alarm in 30 seconds.
while (<$host>) {
last if / 376 /; # We recieved what we wanted to recieve at this point
}
alarm 0; # reset the alarm if we completed within 30 seconds
# Then next block of commands, working pretty much the same way
sub timeout {
error("No response in 30 seconds, timeout.");
}
------------------------
So, I had a series of while loops to wait for the correct answer from the irc
server and so on. There are probably a bunch of oldies that will twist in agony
over the above code, but what the hell - maybe it helps you in some way? Oh,
the error sub just printed the error and then quit the program, nothing fancy.
If you want I could probably code a working example of talking to an IRC
server, and use strict and warnings.