Passing results from sub class of html::parser

G

Gerwin

I'm trying to sub class HTML::parser and store the parsed result in a
variable so I can access it again from outside the sub class. In the
code below the "text" method would print $text AND if I would say
"$vars->{'text_output'} = "test text";" it would also show "test
text" when calling "print $parser->getTextOutput();" but it will not
print the $text when saying "$vars->{'text_output'} = $text;"

What am I doing wrong here?

package MyParser;
use base qw(HTML::parser);

my %vars = (
text_output => undef
);

sub text {
my ($self, $text) = @_;
$vars->{'text_output'} = $text;
}

sub getTextOutput {
return $vars->{'text_output'};
}

package main;
use LWP::Simple;

my $parser = MyParser->new;

my $url = @ARGV[0];

my $html = get $url; die "Couldn't get $url" unless defined $html;

$parser->parse( $html );
$parser->eof;
print $parser->getTextOutput();
 
G

Gerwin

What am I doing wrong here?
package MyParser;
use base qw(HTML::parser);
my %vars = (
text_output => undef
);
sub text {
my ($self, $text) = @_;
$vars->{'text_output'} = $text;
}
sub getTextOutput {
return $vars->{'text_output'};
}
package main;
use LWP::Simple;
my $parser = MyParser->new;
my $url = @ARGV[0];
my $html = get $url; die "Couldn't get $url" unless defined $html;
$parser->parse( $html );
$parser->eof;
print $parser->getTextOutput();

I don't see anything that would put in $MyParser::vars{text_output}
the parsed text. I suppose you may want to implement a parse() method
that would call the base class one AND cache it there. But in any
case, do you really want the cached info to be the same across each
MyParser object?

Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Hmm well the "text" sub routine is called and i figured that if i
stored the parsed text in an extra variable, you could retrieve it
with that get sub routine. I do not know what the best method is for
doing this. I just find it weird that assigning a string to "$vars-
{'text_output'} = "bladiebla";" will return "bladiebla" but assigning
the value "$text" doesn't. Who can shine a light on this?
 
G

Guest

I'm trying to sub class HTML::parser and store the parsed result in a
variable so I can access it again from outside the sub class. In the
code below the "text" method would print $text AND if I would say
"$vars->{'text_output'} = "test text";" it would also show "test
text" when calling "print $parser->getTextOutput();" but it will not
print the $text when saying "$vars->{'text_output'} = $text;"

What am I doing wrong here?

package MyParser;
use base qw(HTML::parser);

my %vars = (
text_output => undef
);

sub text {
my ($self, $text) = @_;
$vars->{'text_output'} = $text;
}

sub getTextOutput {
return $vars->{'text_output'};

This is the wrong syntax. "Vars" is not a hash reference; it's just a hash:
return $vars{'text_output'};



HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top