in reply to Can't locate object method "getElementsByClassName" via package "HTML::TagParser::Element"

I don't see anything before my use of getElementByClassName that would have put us in the context of that subclass
I do ;-) In your first example you have:
my $html = HTML::TagParser->new($line); my @elem = $html->getElementsByClassName($class);
@elem now contains a list of HTML::TagParser::Element objects
In your second example you have:
my $html = HTML::TagParser->new($line); my $body = $html->getElementById($id); my @elem = $body->getElementsByClassName($class);
Here $body is a single HTML::TagParser::Element object, and you can't call getElementsByClassName on that object. Try this (untested):
my $html = HTML::TagParser->new($line); my $body = $html->getElementById($id); $body = $body->subTree(); # $body is now a new HTML::TagParser object my @elem = $body->getElementsByClassName($class);
  • Comment on Re: Can't locate object method "getElementsByClassName" via package "HTML::TagParser::Element"
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Can't locate object method "getElementsByClassName" via package "HTML::TagParser::Element" ( getElementsByClassName and others for HTML::TagParser::Element )
by Anonymous Monk on Nov 27, 2014 at 08:42 UTC

    Nice job zentara, here is a monkey patch

    BEGIN { package HTML::TagParser::Element; sub AUTOLOAD { my ($name) = our $AUTOLOAD =~ /::(\w+)$/; my $method = sub { my $self = shift; return $self->subTree->$name( @_ ); }; no strict 'refs'; * { $AUTOLOAD } = $method; goto &$method; } }

    Now calling getElementsByClassName on a HTML::TagParser::Element works :)

      Sorry about that tangent .. when I get distracted tangent associates to zentara and bam ... kinda miss the guy :)
Re^2: Can't locate object method "getElementsByClassName" via package "HTML::TagParser::Element"
by Vonunov (Novice) on Nov 27, 2014 at 05:04 UTC

    Cool, I see how that works now. Your suggestion does the trick. I think I almost had something working with HTML::TreeBuilder, but as this script is auxiliary and it's getting me what I need, I think I'll leave well enough alone.