Matthew Simoneau
Matthew Simoneau

Reputation: 6279

Trouble with "unblessed reference" when using HTML::TokeParser

I've been poking a this and can't get around this "unblessed reference" error. Here's my simplified code:

#!/usr/local/bin/perl

use strict;
use warnings;
use HTML::TokeParser;

my $p = HTML::TokeParser->new( $ARGV[0] );
while (my $t = $p->get_tag('img')) {
    my $src = $t->get_attr('src');
    print "$src\n";
}

And here's the error message when I try it:

Can't call method "get_attr" on unblessed reference at M:\list_images_in_html.pl line 9.

I gather that somehow it's not recognizing $t as a token object with a get_attr method, but I don't understand why.

Upvotes: 0

Views: 665

Answers (2)

Len Jaffe
Len Jaffe

Reputation: 3484

According to the manual (HTML::TokeParse at MetaCPAN), get_tag() returns an array reference, not an object.

You cannot call get_attr() on a bog standard array ref.

Upvotes: 4

mob
mob

Reputation: 118685

get_attr is a convenience method in HTML::TokeParser::Simple (a wrapper for HTML::TokeParser) but does not exist in HTML::TokeParser.

Replace two lines in your code with this:

use HTML::TokeParser::Simple;
my $p = HTML::TokeParser::Simple->new( $ARGV[0] );

and your script will work.

Upvotes: 1

Related Questions