Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

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

Well, rather than comment on the potential fragility in such parsing schemes, I'll suggest a simplification to your scan() routine (reducing the loc by half+):

sub scan ($) { my $line = shift; while ($line =~ m/<\s*(\w+)([^>]*)>/g) { next unless is_literal($1,$2); my $start = pos($line); my $len = index($line,"</$1>",$start) - $start; my $passage = \substr($line,$start,$len); $$passage = escape_out($$passage); pos($line) = $start; } print $line; }

This uses assignment to pos() at the end of the loop to reset to where we left off so we may continue our match after modifying the string. Also, this uses a reference to the substr() function ... this is a reference to an Lvalue so assigning through the reference changes the substring being pointed to (perhaps a wee bit obfu for production use, but that's your decision :-)

Of course, if the data doesn't follow exactly according to your expectations (a closing </listing > tag for example won't be found because we didn't allow for a trailing space in the closing tag, nor did we check that index() found a closing tag, ...), then all bets are off for your preprocessor (OK, so I did make a fragility comment).

In reply to Re: CDATA-like "literal" tags in XML-like data by danger
in thread CDATA-like "literal" tags in XML-like data by John M. Dlugosz

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.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2023-02-06 10:19 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (33 votes). Check out past polls.