Question

use strict;
use warnings;
use utf8;
use 5.010;
use HTML::HTML5::Parser;

open (FILE, '<links.txt') ;

my @lines = <FILE>;
my $i;
my $a = $lines[$i];

my $xml = HTML::HTML5::Parser->load_html(location => $a) ;

got error: "Can't call method "setValue" on an undefined value at C:/Dwimperl/perl/site/lib/ HTML/HTML5/Parser/TagSoupParser.pm line 2946"

I've tried to insert an if and define value first but it doesn't work.

I'm at the beginning of learning Perl, could anyone here help me with this?

Was it helpful?

Solution

use strict;
use warnings;
use utf8;
use 5.010;
use HTML::HTML5::Parser;
use Try::Tiny;

open (my $FILE, '<', 'links.txt') ;
my @lines = <$FILE>;
my $i = 0;
foreach my $a (@lines) {
    my $xml = try {
        HTML::HTML5::Parser->load_html(location => $a)
    } catch {
        warn "Bad line [$i][$a]";
        warn "Actual error: $_";
    };
    $i++;
}

I've cleaned up your code with my best guess as to how it's actually supposed to be structured, and added a try/catch block (using Try::Tiny) to demonstrate a method of identifying your problem lines.

You need to identify what input is causing the problem before you can really diagnose the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top