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) |