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.


In reply to Ref to an object instance method by former33t

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.