oriented object programming

Discussion in 'Perl Misc' started by franck, Jul 21, 2003.

  1. franck

    franck Guest

    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
    franck, Jul 21, 2003
    #1
    1. Advertising

  2. [posted & mailed]

    On Mon, 21 Jul 2003, franck wrote:

    >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.

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
    Jeff 'japhy' Pinyan, Jul 21, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Miller
    Replies:
    1
    Views:
    491
    Ranagatti
    Sep 19, 2013
  2. Martin
    Replies:
    5
    Views:
    360
    E. Robert Tisdale
    Sep 11, 2003
  3. Joe Mayo
    Replies:
    168
    Views:
    3,301
    David Thompson
    Oct 22, 2007
  4. Replies:
    2
    Views:
    426
    Bruno Desthuilliers
    May 26, 2008
  5. rolo
    Replies:
    3
    Views:
    172
    Robert Klemme
    Apr 9, 2004
Loading...

Share This Page