SPUG: OOP ponderances

Eric Lundberg eric at go2net.com
Wed Jul 12 17:44:26 CDT 2000


I've done things like:

package foo;

sub a_function {
	my ($self, $arg1, $arg2) = @_;
	...
}
 
sub another_foo_function {
	my ($self, $some_other_object) = @_;

	##########################
	# Adding the callback here
        $some_other_object->add_callback( sub { $self->a_function(@_); } );
}


then in some_other_object

package some_other_object

sub add_callback {
	my ($self, $callback) = @_;

        push(@{$self->{CALLBACKS}}, $callback);
}

sub bar {
	my ($self) = @_;

	###########################
	# Calling the callback here
	foreach my $callback (@{$self->{CALLBACKS}}) {
		$callback->($arg1, $arg2, ...);
        }
}

might not be the best way to do it, but the packages all seem to work out 
right (I.e., the callback knows it is of package foo. )

peace,
--e;
        
On Wed, Jul 12, 2000 at 02:05:23PM -0700, Christopher Cavnor wrote:
> 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/
> 
> 

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