#!/usr/bin/perl -w use strict; use XML::Parser; use LWP::Simple; # used to fetch the chatterbox ticker my $message; # Hashref containing infos on a message my $cb_ticker = get("http://perlmonks.org/index.pl?node=chatterbox+xml+ticker"); # we should really check if it succeeded or not my $parser = new XML::Parser ( Handlers => { # Creates our parser object Start => \&hdl_start, End => \&hdl_end, Char => \&hdl_char, Default => \&hdl_def, }); $parser->parse($cb_ticker); # The Handlers sub hdl_start{ my ($p, $elt, %atts) = @_; return unless $elt eq 'message'; # We're only interrested in what's said $atts{'_str'} = ''; $message = \%atts; } sub hdl_end{ my ($p, $elt) = @_; format_message($message) if $elt eq 'message' && $message && $message->{'_str'} =~ /\S/; } sub hdl_char { my ($p, $str) = @_; $message->{'_str'} .= $str; } sub hdl_def { } # We just throw everything else sub format_message { # Helper sub to nicely format what we got from the XML my $atts = shift; $atts->{'_str'} =~ s/\n//g; my ($y,$m,$d,$h,$n,$s) = $atts->{'time'} =~ m/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/; # Handles the /me $atts->{'_str'} = $atts->{'_str'} =~ s/^\/me// ? "$atts->{'author'} $atts->{'_str'}" : "<$atts->{'author'}>: $atts->{'_str'}"; $atts->{'_str'} = "$h:$n " . $atts->{'_str'}; print "$atts->{'_str'}\n"; undef $message; }