1 #!/usr/bin/perl -w 2 use strict; 3 use XML::Parser; 4 use LWP::Simple; # used to fetch the chatterbox ticker 5 6 my $message; # Hashref containing infos on a message 7 8 my $cb_ticker = get("http://perlmonks.org/index.pl?node=chatterbox+xml+ticker"); 9 # we should really check if it succeeded or not 10 11 my $parser = new XML::Parser ( Handlers => { # Creates our parser object 12 Start => \&hdl_start, 13 End => \&hdl_end, 14 Char => \&hdl_char, 15 Default => \&hdl_def, 16 }); 17 $parser->parse($cb_ticker); 18 19 # The Handlers 20 sub hdl_start{ 21 my ($p, $elt, %atts) = @_; 22 return unless $elt eq 'message'; # We're only interrested in what's said 23 $atts{'_str'} = ''; 24 $message = \%atts; 25 } 26 27 sub hdl_end{ 28 my ($p, $elt) = @_; 29 format_message($message) if $elt eq 'message' && $message && $message->{'_str'} =~ /\S/; 30 } 31 32 sub hdl_char { 33 my ($p, $str) = @_; 34 $message->{'_str'} .= $str; 35 } 36 37 sub hdl_def { } # We just throw everything else 38 39 sub format_message { # Helper sub to nicely format what we got from the XML 40 my $atts = shift; 41 $atts->{'_str'} =~ s/\n//g; 42 43 my ($y,$m,$d,$h,$n,$s) = $atts->{'time'} =~ m/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/; 44 45 # Handles the /me 46 $atts->{'_str'} = $atts->{'_str'} =~ s/^\/me// ? 47 "$atts->{'author'} $atts->{'_str'}" : 48 "<$atts->{'author'}>: $atts->{'_str'}"; 49 $atts->{'_str'} = "$h:$n " . $atts->{'_str'}; 50 print "$atts->{'_str'}\n"; 51 undef $message; 52 } #### #!/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; }