Scroll to navigation

XML::SAX::EventMethodMaker(3pm) User Contributed Perl Documentation XML::SAX::EventMethodMaker(3pm)


XML::SAX::EventMethodMaker - SAX event names, creation of methods from templates


version 0.46


    use XML::SAX::EventMethodMaker qw(
        sax_event_names missing_methods compile_methods
  ## Getting event names by handler type and SAX version
    my @events          = sax_event_names;
    my @dtd_events      = sax_event_names "DTDHandler";
    my @sax1_events     = sax_event_names 1;
    my @sax1_dtd_events = sax_event_names 1, "DTDHandler";
  ## Figuring out what events a class or object does not provide
    my @missing = missing_methods $class, @events ;
  ## Creating all SAX event methods
    compile_methods $class, <<'TEMPLATE_END', sax_event_names;
    sub <EVENT> {
        my $self = shift;
        ... do something ...
        ## Pass the event up to the base class
        $self->SUPER::<EVENT>( @_ );
  ## Creating some methods
    compile_methods $class, <<'TEMPLATE_END', @method_names;
  ## Creating only missing event handlers
    compile_missing_methods $class, <<'TEMPLATE_END';


In building SAX machines, it is often handle to build a set of event handlers from a common template. This helper library (or class) provides the database of handler names, queryable by type, and


XML::SAX::EventMethodMaker - SAX event names, creation of methods from templates


    my @names = sax_event_names @query_terms;

Takes a list of query terms and returns all matching events.

Query terms may be:
- a SAX version number: 1 or 2 (no floating point or ranges)
- Handler
- DTDHandler
- ContentHandler
- DocumentHandler
- DeclHandler
- ErrorHandler
- EntityResolver
- LexicalHandler

In addition to normal SAX events, there are also "parse" events:
- ParseMethods

Unrecognized query terms cause exceptions.

If no query terms are provided, then all event names from all versions are returned except for parse methods (parse, parse_uri, ...).

If any version numbers are supplied, then only events from those version numbers are returned. No support for noninteger version numbers is provided, nor for ranges. So far, only two SAX versions exist in Perl, 1 and 2.

If any handler types are provided, then only events of those types are returned. Handler types are case insensitive.

In other words, all returned events must match both a version number and a handler type.

No support for boolean logic is provided.

    my @missing = missing_methods __PACKAGE__, @event_names;
    my @missing = missing_methods $object, @event_names;

This subroutine looks to see if the object or class has declared event handler methods for the named events. Any events that haven't been declared are returned.

It is sufficient to use subroutine prototypes to prevent shimming AUTOLOADed (or otherwise lazily compiled) methods:

    sub start_document ;
    compile_methods __PACKAGE__, $template, @method_names;
    compile_methods $object,     $template, @method_names;

Compiles the given template for each given event name, substituting the event name for the string <EVENT> or <METHOD> in the template. There is no difference between these two tags, they are provided to only to let you make your templates more readable to you.

    compile_missing_methods __PACKAGE__, $template, @method_names;
    compile_missing_methods $objects,    $template, @method_names;

Shorthand for calls like

    compile_methods __PACKAGE__, $template,
        missing_methods __PACKAGE__, @method_names;

Due Credit

The database of handlers by type was developed by Kip Hampton, modified by Robin Berjon, and pilfered and corrupted by me.


    Database Copyright 2002, Barrie Slaymaker, Kip Hampton, Robin Berjon
    Code Copyright 2002, Barrie Slaymaker <>

You may use this under the terms of the Artistic, GNU Public, or BSD licences, as you see fit.


  • Barry Slaymaker
  • Chris Prather <>


This software is copyright (c) 2013 by Barry Slaymaker.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

2022-06-28 perl v5.34.0