Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Your own Chatterbox nodelet in less than 50 lines of code.

by Dog and Pony (Priest)
on May 20, 2002 at 12:22 UTC ( #167816=snippet: print w/ replies, xml ) Need Help??

Description: (Well, that is, if you strip out the comments and blank lines = 42 lines). :)

Really only a good example on why you should use available modules. This is a Chatterbox "nodelet", much like the one you can see in the fullpage chat. It uses LWP::Simple to fetch the XML, XML::Simple to parse the content and CGI.pm to present the result.

It does not handle the [something] type links, but that should be possible to add with not too much trouble. Otherwise it pretty much mimicks the standard behaviour, and it does not add random whitespace here and there (what I've noted so far). :)

All written in a very short time, courtesy of the really cool people that write excellent modules and then distribute them. Which was what I wanted to show. :)

As usual, there is a place to take a test spin.

Update: Fixed stupid /me display bug.

Update 2: Changed title to something less confusing, as per request.

Update 3: Lots of thanks to wil for noticing that I had a forcearray bug and pointed this out to me. The code has now been updated with wil's suggestion.

#!/usr/bin/perl -w
use strict;
use CGI;
use XML::Simple;
use LWP::Simple;

my $q = CGI->new;

# Base URL
my $pm = 'http://www.perlmonks.org/';

# Chatterbox ticker:
my $ticker = $pm . '?node_id=15834';

# Start HTML:
print $q->header;
print $q->start_html(-title => 'Chatterbox',
                     -head  => $q->meta({-http_equiv => 'refresh',
                                         -content    => '12',
                                         -url        => $q->self_url})
+);
# Get the chat XML:
my $chat = get $ticker;

# Mangle it through XML::Simple
my $xml = XMLin($chat, forcearray => 1);

# Get the messages part:
my $messages = $xml->{'message'};

# Is there anyone chatting?
if($messages)
{
    # All messages in the chatterbox currently
    foreach my $msg (@$messages)
    {
        my $author =  $msg->{'author'};
        my $content = $msg->{'content'};
        my $user_id = $msg->{'user_id'};
        
        # Strip leading whitespace...
        $content =~ s/^\s*//;

        # Compose the url for the author:
        my $author_url = $q->a({-href => "$pm?node_id=$user_id"}, $aut
+hor);

        # Is the authour using '/me '?
        if($content =~ s{^/me }{})
        {
            print $q->i("$author_url $content");
        }
        # Nope, normal chatter:
        else
        {
            print "<$author_url> $content";
        }
        print $q->br, "\n";
    }
}

# Nope, all is... boring?
else
{
    print $q->i('and all is boring...');
}

# End HTML:
print $q->end_html;


Comment on Your own Chatterbox nodelet in less than 50 lines of code.
Download Code
(jeffa) Re: Your own Chatterbox nodelet in less than 50 lines of code.
by jeffa (Chancellor) on May 20, 2002 at 23:38 UTC
      Mine and mirod's were under 20 lines
      Yeah, but you are cheating by putting the braces on the same line as the sub name and whatnot. ;-)

      Seriously though, nice to see more examples. Those modules really makes hard things really simple sometimes. I was insanely surprised when I discovered how XML::Simple worked - it did what? Create a data structure you can just burrow into that easily? It was amazing. :)

      Having just discovered that XML::Simple was just as simple to use as LWP::Simple, I was very happy about this fact and wanted to whip up a small demonstration of how easy things can be if you just use modules. By choosing to (poorly) imitate the chatterbox nodelet, I think I covered quite a broad spectrum, with a few good modules and very little code. Or effort, for that matter. :)

      It was also very rewarding when someone posted the html code for the "sexisgood" submit button in the chatterbox, and it showed up in the middle of the chat in my version. I'm thinking to never fix that. :)


      You have moved into a dark place.
      It is pitch black. You are likely to be eaten by a grue.

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://167816]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2014-04-23 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (555 votes), past polls