former33t has asked for the wisdom of the Perl Monks concerning the following question:

Oh esteemed monks,

I have a problem where I need to pass a code reference to a custom SAX parser I wrote. The idea is to be able to swap out what is done by changing the callback function. I'm able to pass a callback to a package method, but not an object instance method. In this trivial example, it doesn't matter, but one of the functions I want to put in another callback involves databasing the information. I could put the database handler in a global, but that violates the concept of encapsulation.

MIS_SAX_Parser.pm

package MIS_SAX_Parser; use Data::Dumper; use XML::SAX::Base; @ISA = ('XML::SAX::Base'); use strict; use constant TYPE0 => 0; use constant TYPE1 => 1; use constant TYPE2 => 2; 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; } sub start_element { my ($self, $data) = @_; if ($data->{Name} eq "Column") { $self->{COLUMN_DATA}->{$data->{Attributes}->{"{}name"}->{Value}} = $ +data->{Attributes}->{"{}value"}->{Value}; } elsif ($data->{Name} eq "Table") { $self->{TABLE_NAME} = $data->{Attributes}->{"{}name"}->{Value}; } } sub end_element { my ($self, $data) = @_; my $name = $data->{Name}; if ($name eq "Table") { $self->{CALLBACK}->($self->{TABLE_NAME}, $self->{COLUMN_DATA}); # Reset the TABLE_NAME and COLUMN_DATA $self->{TABLE_NAME} = undef; $self->{COLUMN_DATA} = {}; } } # Debug callback function sub nocall { my ($table, $data) = @_; print "Table name is $table\n"; print Dumper($data)."\n\n"; } 1;

MIS_PrintStatus.pm

package MIS_PrintStatus; use Data::Dumper; use strict; use warnings; sub new { my ($class) = @_; my $self = {}; bless $self, $class; return $self; } sub printit { my ($table, $data) = @_; print "The table name is $table\n"; } 1;

MISParse.pl

#!/usr/bin/perl use XML::LibXML; use XML::LibXML::SAX::Parser; use MIS_SAX_Parser; use MIS_PrintStatus; use strict; my $filename = shift @ARGV; my $cb = MIS_PrintStatus->new(); my $parser = XML::LibXML->new; my $doc = $parser->parse_file($filename); my $handler = MIS_SAX_Parser->new(\&MIS_PrintStatus::printit); my $generator = XML::LibXML::SAX::Parser->new(Handler => $handler); $generator->generate($doc);

Now what I'd like to do is be able to pass a reference to $cb->printit to the MIS_SAX_Parser object. Anyone know the syntax for this? I tried what I thought were all the usual suspects and only got errors. Thanks.

Replies are listed 'Best First'.
Re: Ref to an object instance method
by Corion (Patriarch) on Dec 03, 2007 at 22:12 UTC

    The trick is to wrap the method invocation in a subroutine:

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

    Basically, you're creating a curried version of your method invocation, since $cb->printit is to Perl mostly MIS_PrintStatus::printit( $cb ).

Re: Ref to an object instance method
by webfiend (Vicar) on Dec 03, 2007 at 22:16 UTC

    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; }
Re: Ref to an object instance method
by moritz (Cardinal) on Dec 03, 2007 at 22:13 UTC
    I don't know off-hand how to do it "right", but you can always create a closure:
    my $callback = sub { $object->method(@_); };
Re: Ref to an object instance method
by stvn (Monsignor) on Dec 04, 2007 at 02:09 UTC

    All the above suggestions are fine, but if you want this really to be a feature of your objects, then you could add method currying features to a base class pretty easily ...

    package Curryable; sub curry { my ($self, $method_name, @args) = @_; my $method = $self->can($method_name) || die "No $method_name meth +od found"; return sub { $self->$method(@args, @_) }; }
    Then have your MIS_PrintStatus class inherit from Curryable and then all you need to do is this ...
    my $handler = MIS_SAX_Parser->new($cb->curry('printit'));
    And it will make sure to wrap up the method correctly for you. If you need some additional flexibility, you could also define a "right curry" method as well, which can come in quite handy. Note the reversal of the @args and @_ from the original curry.
    sub rcurry { my ($self, $method_name, @args) = @_; my $method = $self->can($method_name) || die "No $method_name meth +od found"; return sub { $self->$method(@_, @args) }; }

    -stvn
      Thanks much for the suggestion. I think this is more along the lines of what I was looking for. I'll work on implementing this today in the non-trivial example.