-3

I'm trying to follow the example in the synopsis of HTML::PrettyPrinter. I corrected the typo to create a FileHandle:

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

Now the file gets created but I'm getting another error:

Can't call method "isa" on an undefined value at C:/Strawberry/perl/site/lib/HTML/PrettyPrinter.pm line 414.

Here is the code I have so far:

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),  
}
5
  • Do you have use strict; use warnings; at the top of your script?
    – Tom Fenech
    May 30, 2014 at 12:16
  • Well, you should! It won't solve your problem automatically but may give some clues as to what is happening.
    – Tom Fenech
    May 30, 2014 at 12:17
  • Does it die when $hpp->format($tree||die 'no $tree');?
    – mpapec
    May 30, 2014 at 12:23
  • its prints in single line now @mpapec May 30, 2014 at 12:27
  • I mean all the errors are gone once I pass the line you provided in the filehandling area. but indension doesn't work instead it prints the whole thing in single line May 30, 2014 at 12:30

1 Answer 1

4

This line is causing the error:

$hpp->format();

HTML::PrettyPrinter::format attempts to call isa on the first argument:

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

Which causes the error you're getting if it is undef. Passing $tree (which isa HTML::Element) as the first argument populates the file correctly:

$hpp->format($tree);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.