oriented object programming

F

franck

Greetings

I have the following class:

#!/usr/bin/perl

package monParser;
use HTML::parser;
@ISA = qw(HTML::parser);


my $ligne;
my $flag = 0;
my %want = (tag => 'font',
attrs => { face => "Arial", size => "2" });

sub start_handler {
my($tag, $attr) = @_;
return unless $tag eq $want{tag};
foreach my $name ( keys %{$want{attrs}} ) {
return unless $attr->{$name} eq $want{attrs}{$name};
}; $flag = 1;
}

sub new
{
my ($class) = @_;
my $this = $class -> SUPER::new(api_version => 3,
start_h => [\&start_handler, "tagname, attr"],
end_h => [sub {$flag=0 if shift eq $want{tag}},
"tagname"],
text_h => [sub {$ligne.= shift, "\n" if $flag}, "dtext"],
);

$this -> {'LIGNE'} = $ligne;
bless ($this,$class);
return $this;
}

sub affiche
{
my ($this) = @_;
print $this->{'LIGNE'};
}
1;


I use this class in this script:

#!/usr/bin/perl -I /home/Perl/Programmes//module
use monParser;
my $p = monParser->new();
$p->parse_file("\/home\/Perl\/Programmes\/arrivee\/tempo\.txt") || die $!;
$p->affiche;

I would like to print the LIGNE attibut with the "affiche" method
It does not work !

What is the problem ?

Thanks.
Franck
 
J

Jeff 'japhy' Pinyan

[posted & mailed]

package monParser;
use HTML::parser;
@ISA = qw(HTML::parser);


my $ligne;

You defined $ligne here, but it doesn't have a value...
my $flag = 0;
my %want = (tag => 'font',
attrs => { face => "Arial", size => "2" });

sub start_handler {
my($tag, $attr) = @_;
return unless $tag eq $want{tag};
foreach my $name ( keys %{$want{attrs}} ) {
return unless $attr->{$name} eq $want{attrs}{$name};
}; $flag = 1;
}

sub new
{
my ($class) = @_;
my $this = $class -> SUPER::new(api_version => 3,
start_h => [\&start_handler, "tagname, attr"],
end_h => [sub {$flag=0 if shift eq $want{tag}},
"tagname"],
text_h => [sub {$ligne.= shift, "\n" if $flag}, "dtext"],
);

$this -> {'LIGNE'} = $ligne;

Here you've assigned $ligne to $this->{LIGNE}, but it still has no value.
Perhaps you wanted to get it from $_[1]?
bless ($this,$class);
return $this;
}
$p->parse_file("\/home\/Perl\/Programmes\/arrivee\/tempo\.txt") || die $!;

Is that argument supposed to be the value put in the LIGNE attribute? And
why are you backslashing everything?

$p->parse_file(/home/Perl/Programmes/arrivee/tempo.text") or die $!;

works just fine.
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top