I’ve got an XML::SAX::Base-based parser that looks something like this:
package MyParser;
use base qw(XML::SAX::Base);
our @ISA = ('XML::SAX::Base');
sub new {
my $class = shift;
my %params = @_;
my $self = {
thing => $params{thing},
};
bless $self, $class;
return $self;
}
sub start_element {
my ($self, $data) = @_;
# Do something useful using $data & $self->{thing}
}
1;
And I’m using it with XML::SAX::ParserFactory:
#!/usr/bin/env perl
use MyParser;
my $parser = XML::SAX::ParserFactory->parser(
Handler => MyParser->new
);
$parser->parse_string('<document/>');
Is there a way that I can pass arguments to MyParser’s new() subroutine?
Your
newmethod is already set up to receive arguments using%params, so just pass your arguments as a hash:Then in your
newmethod you can access the arguments as you would any other hash: