SPUG: OOP ponderances

Christopher Cavnor chris at enthusiasm.com
Wed Jul 12 16:05:23 CDT 2000


Need some assistance from the OOP people in the audience...

I have a class (boiled-down code below), let's call it "Test". All is
fine with the class until I call the "walker" method from the "gather"
method. What I want is for the "gather" method to know that it is a
member of class Test, but (I assume because of the callback syntax), it
sees itself as a member of HTML::Element (inherited by
HTML::TreeBuilder).

How do I pass a reference to class Test in the callback?

Thanks for any help;
Chris

-------------------------------------------
Succinct and fallible code digest below
-------------------------------------------


package Test;

sub new {
      my ($class,   $email) = @_;

      my $self = {};
      bless $self, $class;

      # Create a User Agent object, give it a name, email ID
      $self->{'ua'} = new LWP::RobotUA 'Enthusiasm_EventScanBot/1.0',
$email;

        # Create hash to record nodes
        $self->{'record'} = {};

      return $self;
}

...
#call gather_links($self, $data);
...

sub gather {
    require HTML::TreeBuilder;

      my ($self, $data) = @_;
      my @urls;

      $self->{'tree'} = HTML::TreeBuilder->new; # empty tree
      $self->{'tree'}->parse($data);

          for (@{  $self->{'tree'}->extract_links('a')  }) {
                my($link, $element) = @$_;
                push @urls, $link;
            }

      #walk the tree to find event info
      $self->{'tree'}->traverse(\&walker); #<-----this imfamous callback
routine
      #dispose of tree
      $self->{'tree'}->delete;

  return @urls;
}

sub walker {
    my ( $self, $node, $start, $depth, $text_parent, $rel_index ) = @_;

    # this should report $self as an instance of  package test, but
reports HTML::Element
    # which is a class that HTML::TreeeBuilder inherits from.

    $self->{'record'}{$node} = 1;     #this would fail

    return 1; #keep recursing
}



 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For full traffic, use spug-list for LIST ; otherwise use spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list