The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

Name

XML::Parser::Expat::Dispatched

Version

Version 0.9

Synopsis

    package MyParser;
    use parent XML::Parser::Expat::Dispatched;
    
    sub Start_tagname{
      my $self = shift;
      print $_[0], $self->original_tagname;
    }
    
    sub End_tagname{
      my $self=shift;
      print "$_[0] ended";
    }
    
    sub Char_handler{
      my ($self, $string) = @_;
      print $string;
    }
     
     sub transform_gi{
      lc $_[1];
     }


     package main;
     my $p = MyParser->new;
     $p->parse('<Tagname>tag</Tagname>');

Description

You simply write subroutines. Your parser will be a XML::Parser::Expat so consider checking the Methods of this class if you write other methods than handler methods. The underscore in the subroutine names is optional for all but the transform_gi method. The arguments your subroutine gets called with are the same as those for the handlers from XML::Parser::Expat. This package provides a new method that produces some dispatch methods after reading the symbol table of your module, so be sure that your subroutines are actually in there when making the call to new. These will then be installed into the handlers of an XML::Parser::Expat. If you want to write your own new method, make sure that this modules new method get's called.

Since your package will inherit XML::Parser::Expat be prepared to call it's release-method if you write your own DESTROY-method.

Start_tagname

Will be called when a start-tag is encountered that matches tagname. If tagname is not given (when your sub is called Start or Start_), it works like a default-handler for start tags.

End_tagname

Will be called when a end-tag is encountered that matches tagname. If tagname is not given (when your sub is called End or End_), it works like a default-handler for end tags.

Handler_handler

Installs this subroutine as a handler for XML::Parser::Expat. You can see the Handler names on XML::Parser::Expat. Notice that if you try to define a handler for Start or End, they will be interpreted as Start or End handlers for handler-tags, use subs called Start or End instead.

transform_gi (Parser, Suffix/Tagname, isSuffix)

This subroutine is special: you can use it to generalize the check between the subroutine suffix for the Start* and End* subroutine names and the tagnames.

Some Examples:

    sub transform_gi{lc $_[1]}                            # case insensitive
    sub transform_gi{return !$_[2] && $_[1]=~/:([^:]+)$/?$1: $_[1]} # try discarding the namespace

Notice that the allowed characters for perl's subroutines and XML-Identifiers aren't the same so you might want to use the default handlers or transform_gi.

Diagnostics

  the sub %s1 overrides the handler for %s2

You most probably have two subroutines that have the same name exept one with an underscore and one without. The warning issued tells you wich subroutine will be used as a handler. Since the underlying mechanism is based on the each iterator, this behavior can vary from time to time playing, so you might want to change your sub names.

  %s1 and %s2 translate to the same handler

There is an sub called %s1 that translates to the same handler as a sub %s2 after applying transform_gi. The sub %s1 will be used.

If you overwrite __gen_dispatch this module doesn't work.

Author

Patrick Seebauer

Licence

This software is Copyright (c) 2013 by Patrick Seebauer.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)