Pretty Printer perl模块无法按预期工作

时间:2014-05-30 12:07:34

标签: html perl indentation

我试图在HTML::PrettyPrinter的概要中遵循这个例子。我更正了拼写错误以创建FileHandle

my $fh = new FileHandle ">E:\\test.html";

现在文件已创建,但我收到了另一个错误:

  

无法在C:/Strawberry/perl/site/lib/HTML/PrettyPrinter.pm第414行调用未定义值的方法“isa”。

这是我到目前为止的代码:

use HTML::TreeBuilder;
# generate a HTML syntax tree
my $tree = new HTML::TreeBuilder;
$tree->parse_file("E:\\file.html");
# modify the tree if you want

use HTML::PrettyPrinter;
my $hpp = new HTML::PrettyPrinter ('linelength' => 130,'quote_attr' => 1);
# configure
$hpp->set_force_nl(1,qw(body head));             # for tags
$hpp->set_force_nl(1,qw(@SECTIONS));             # as above
$hpp->set_nl_inside(0,'default!');               # for all tags

# format the source
my $linearray_ref = $hpp->format($tree);
print @$linearray_ref;

# alternative: print directly to filehandle
use FileHandle;
my $fh = new FileHandle ">E:\\test.html";
if (defined $fh) {
  $hpp->select($fh);
  $hpp->format();
  undef $fh;
  $hpp->select(undef),  
}

1 个答案:

答案 0 :(得分:4)

此行导致错误:

$hpp->format();

HTML::PrettyPrinter::format尝试在第一个参数上调用isa

411 sub format {
412  my ($self, $element, $indent, $lar) = @_;
413  # $lar = line array ref
414  confess "Need an HTML::Element" unless $element->isa('HTML::Element');
     ...

如果是undef,则会导致错误。将$treeisa HTML::Element)作为第一个参数传递正确填充文件:

$hpp->format($tree);