The look_down method is not finding the DOCTYPE declaration although it is present in Data::Dumpers output as described in the
docs. What's the best way to do this?
#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
use HTML::TreeBuilder;
my $content = do{local $/;<DATA>};
my $root = HTML::TreeBuilder->new_from_content($content);
print $root->tag, qq{\n};
# prints: html
print $root->{_decl}{_tag}, qq{\n};
# prints: ~declaration
print $root->{_decl}{text}, qq{\n};
# prints: DOCTYPE html PUBLIC "XHTML"
my $dec = $root->look_down(
_tag => q{~declaration},
) or die qq{declaration not found\n};
$dec->splice_content(
0,
1,
q{<!DOCTYPE html PUBLIC "HTML4">},
) or die qq{splice failed\n};
print $root->as_HTML;
__DATA__
<!DOCTYPE html PUBLIC "XHTML">
<html>
<head><title>declaration</title></head>
<body><p>declaration</p></body>
</html>
extract from Data::Dumper output
'_decl' => bless( {
'_tag' => '~declaration',
'text' => 'DOCTYPE html PUBLIC "XHTML"'
}, 'HTML::Element' ),
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.