Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Ref to an object instance method

by webfiend (Vicar)
on Dec 03, 2007 at 22:16 UTC ( #654668=note: print w/replies, xml ) Need Help??

in reply to Ref to an object instance method

I'm fond of wrapping the method call into a subroutine reference.

my $cb = MIS_PrintStatus->new(); my $callback = sub { $cb->printit(); }; # ... my $handler = MIS_SAX_Parser->new($callback);

Update: I typed too slow and others got this answer before me. I'll try to add some value to this comment by pointing out that you can streamline your constructor:

# Here's what you have. sub new { my ($class, $callback) = @_; my $self = { COUNTER => 0, HASHTYPE => TYPE0 }; bless $self, $class; if ($callback) { $self->{CALLBACK} = \&$callback; } else { $self->{CALLBACK} = \&nocall; } return $self; } # Start with this instead, maybe sub new { my ($class, $callback) = @_; $callback ||= \&nocall; my $self = { COUNTER => 0, HASHTYPE => TYPE0, CALLBACK => $callback, }; bless $self, $class; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://654668]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2019-07-19 12:46 GMT
Find Nodes?
    Voting Booth?

    No recent polls found