Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??
(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;

In reply to Your own Chatterbox nodelet in less than 50 lines of code. by Dog and Pony

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.