Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Greeting fellow monks,

I was about to ask something in chatterbox using Shendal's monkchat when I found it just didn't work. It didn't show the currently online users and showed no messages at all. "This must be because of recent changes of this site layout", I thought, then I grabbed the latest PerlMonks modules (version 2.2), install it, but the same problem was still there.

After "super searching", I only found the same problem (Does anybody else have this problem?) was reported but no response.

After looking into PerlMonks modules, I found a fundamental problem that the XML parsing is done using regex in a fragile way, that's why it is broken. Here is a quick fix to PerlMonks/Chat.pm and PerlMonks/Users.pm, which simply replaces that parsing part with XML::Simple's XMLin().

--- Chat.pm-old Tue Sep 24 17:45:12 2002 +++ Chat.pm Tue Sep 24 17:32:07 2002 @@ -11,6 +11,7 @@ use strict; use vars qw(@ISA); use HTML::Entities; +use XML::Simple; use PerlMonks; use PerlMonks::NewestNodes; @@ -58,7 +59,15 @@ # Get general chat messages if ($c=$self->getpage(CHAT_URL)) { $c=~s/[\r\n\t]//g; - my @msgs=($c=~/message\s+author="([^\"]+)"[^>]+>\s*(.*?)\s*<\/mes +sage>/g); + + # problematic + # my @msgs=($c=~/message\s+author="([^\"]+)"[^>]+>\s*(.*?)\s*<\/m +essage>/g); + + my $msgs = XMLin($c, forcearray => 1)->{message}; + + my @msgs = map { $_->{author} => $_->{content} } + sort { $a->{time} <=> $b->{time} } $msgs ? @$msgs : (); + if (@msgs) { while (@msgs) { my ($author, $msg)=(shift(@msgs),shift(@msgs));
--- Users.pm-old Tue Sep 24 17:45:18 2002 +++ Users.pm Tue Sep 24 17:37:28 2002 @@ -14,6 +14,7 @@ use strict; use vars qw(@ISA); +use XML::Simple; use PerlMonks; @ISA=qw(PerlMonks); @@ -63,7 +64,13 @@ my $self=shift; if ( (time() - $self->{cache_users_ts}) > USERS_REFRESH) { if (my $c=$self->getpage(USERS_URL)) { - my %users=($c=~/user\s+username="([^\"]+)"\s+user_id="(\d+)"/g) +; + + # problematic + # my %users=($c=~/user\s+username="([^\"]+)"\s+user_id="(\d+)" +/g); + + my $users = XMLin($c, forcearray => 1)->{user}; + my %users = map { $_->{username} => $_->{user_id} } $users ? @ +$users : (); + $self->{cache_users}=\%users; $self->{cache_users_ts}=time(); }

In reply to Quick Fix for PerlMonks modules by pope

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (11)
    As of 2015-07-29 08:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (262 votes), past polls