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' ),
-
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.