Use HTML::TokeParser::Simple. First, I designed it to be a drop-in replacement, so you could use the module without any changes in your code except for the use statement and the constructor call. Then, as you refactor, you use the module to 'clean up' the nasty bits. The following is untested and assumes that the token is from HTML::TokeParser::Simple. It also requires the latest version because the "as_is" method is new.

my $tag; if ($change) { my $tag_type = $token->return_tag; my $attributes = make_attributes($token); $tag = "<$tag_type $attributes>"; } else { $tag = $token->as_is; } sub make_attributes { my $token = shift; my $seq = $token->return_attrseq; my $attr = $token->return_attr; return join ' ', map {qq|$_="$attr{$_}"|} @$seq; }

My version will also preserve the attribute sequence. Also note the the token, as mentioned, is still an array reference, so you can access them if you must, or do everything simpler by using the supplied methods. Since the token data is also the instance data, if you change the arrayref's data directly, you are also changing the instance data, which is why the above code still work.

Hmm... this gives me more ideas of what could be included in the module.

Cheers,
Ovid

New address of my CGI Course.
Silence is Evil (feel free to copy and distribute widely - note copyright text)


In reply to Re: Rebuilding changed tags with HTML::TokeParser by Ovid
in thread Rebuilding changed tags with HTML::TokeParser by swiftone

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.