former33t has asked for the wisdom of the Perl Monks concerning the following question:
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 | |
|
Re: Ref to an object instance method
by webfiend (Vicar) on Dec 03, 2007 at 22:16 UTC | |
|
Re: Ref to an object instance method
by moritz (Cardinal) on Dec 03, 2007 at 22:13 UTC | |
|
Re: Ref to an object instance method
by stvn (Monsignor) on Dec 04, 2007 at 02:09 UTC | |
by former33t (Scribe) on Dec 04, 2007 at 13:56 UTC |