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


Chess::PGN::Filter - Perl extension for converting PGN files to other formats.


 use Chess::PGN::Filter;

 filter(source => $pgn,filtertype => 'XML');


 my %substitutions = (
     hsmyers => 'Myers, Hugh S (ID)',

 my @exclude = qw(

     source => $pgn,
     filtertype => 'TEXT',
     substitutions => \%substitutions,
     nags => 'yes',
     exclude => \@exclude,


     source => $pgn,
     filtertype => 'DOM',


 $dom = filter(
     source => $pgn,
     filtertype => 'DOM',
     verbose => 0,


This is a typical text in one side, different text out the otherside filter module. There are as of this writing, the following supported choices:

1. XML -- Converts from .pgn to .xml using the included pgn.dtd as the validation document. This is for the most part a one to one transliteration of the PGN standard into XMLese. It does have the additional virtue of allowing positions to be encoded within the XML output. These are generated by an embedded NAG of {0} and automatically (user controlled) at the end of each game. As a kind of adjunct to the position diagrams, pgn.dtd optionally allows each move to include it's FEN string. This allows scripted animation for web pages generated this information.
2. TEXT -- Although the PGN standard is widely available, many program that generate .pgn do so in an ill-formed way. This mode is an attempt to 'normalize' away the various flaws found in the 'wild'! This includes things like game text all on a single line without a preceding blank line. Or castling indicated with zeros rather than the letter 'O'. There is at least one application that carefully indents the first move! The list of oddities is probably as long as the list of applications.
3. DOM -- A Document Object Model (DOM) makes for a very convenient interim form, common to all other filter types. Useful in both the design and debugging phases of filter construction. By way of self-documentation, here is an example of a single game that shows all of the obvious features of the DOM:
 $VAR1 = {
          'Tags' => {
                      'Site' => 'Boise (ID)',
                      'Event' => 'Cabin Fever Open',
                      'Round' => '1',
                      'ECO' => '?',
                      'Date' => '1997.??.??',
                      'White' => 'Barrett Curtis',
                      'Black' => 'Myers Hugh S',
                      'Result' => '1-0'
          'Gametext' => [
                            'Movenumber' => '1',
                            'Epd' => 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3',
                            'Movetext' => 'e4'
                            'Movenumber' => '2',
                            'Epd' => 'rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq d6',
                            'Movetext' => 'd5'
                            'Movenumber' => '3',
                            'Epd' => 'rnbqkbnr/ppp1pppp/8/3pP3/8/8/PPPP1PPP/RNBQKBNR b KQkq -',
                            'Movetext' => 'e5'
                            'Movenumber' => '4',
                            'Comment' => 'Playing ...Bf5 before closing the c8-h3 diagonal has  some positive features.',
                            'Epd' => 'rnbqkbnr/ppp2ppp/4p3/3pP3/8/8/PPPP1PPP/RNBQKBNR w KQkq -',
                            'Movetext' => 'e6'
                            'Movenumber' => '5',
                            'Epd' => 'rnbqkbnr/ppp2ppp/4p3/3pP3/3P4/8/PPP2PPP/RNBQKBNR b KQkq d3',
                            'Movetext' => 'd4'
                            'Movenumber' => '6',
                            'Comment' => 'Time to think like a Frenchie - c7-c5!',
                            'Epd' => 'r1bqkbnr/ppp2ppp/2n1p3/3pP3/3P4/8/PPP2PPP/RNBQKBNR w KQkq -',
                            'Movetext' => 'Nc6',
                            'Rav' => [
                                         'Movenumber' => '6',
                                         'Epd' => 'rnbqkbnr/pp3ppp/4p3/2ppP3/3P4/8/PPP2PPP/RNBQKBNR w KQkq c6',
                                         'Movetext' => 'c5'
                            'Movenumber' => '29',
                            'Comment' => ' (Bxe5) Black could  still kick for a while if he had played ...Bxe5.',
                            'Epd' => 'r1bq1rk1/2p1npb1/2n1p2P/pp1pP1p1/3P2P1/2P4Q/PP2BP2/RNB1K2R b KQ -',
                            'Movetext' => 'h6'

Briefly, the DOM is a multiply nested data structure of hashes and arrays. In a sort of outline form, it more or less follows this schematic:

I PGN Document Root
A. Extra-Game Comments
1. Before 1st Game
2. After Each Game
B. Games
1. Tagset
2. Extra-Gametext Comments
3. Gametext
a. Moves
1.) Movetext
2.) Comment
3.) NAG
4.) RAV (essentially an instance of Gametext)

The 'extra' comments have not yet been implemented. See the TODO list.

Owing to a dearth of imagination, there is but one exported routine in the module:


There are however, a small host of known keys for parameter_hash and they are as follows:

  • keys common to all filtertypes

    • verbose -- switch between output to STDOUT and output returned as an ARRAY refference. Defaults to 1 and sends output to STDOUT. The code for this patch comes from Gene Boggs [] for which my thanks!

    • filtertype -- essentially which filter to use. Values implemented are:

      1. XML -- converts from .pgn text in, to .xml file out. Validated by supplied pgn.dtd.
      2. TEXT -- converts from .pgn text in, to .pgn out with reformatting of ill-formed text and other modifications possible. Global correction of tag values, error checking for game text termination etc. Blank lines and paragraph wrapping emplemented to match PGN standard.
      3. DOM -- converts from .pgn text to a Document Object Model as expressed using Data::Dumper.
    • source -- name of file to convert, with output sent to STDOUT.

  • keys for filtertype TEXT

    • substitute -- simple text substitution mechanism applied globally (file scope) to all tag text.

      This is actually a hash reference where the hash reffered to has the form of (text_to_change => text_to_change_to). For instance:

       my %substitutions = (
           hsmyers => 'Myers, Hugh S (ID)'

      as used in the SYNOPSIS example would expand my user name into a full version for any tag the former might occur in.

    • comments -- switch to include/exclude comments (defaults to 'no'.)

    • ravs -- switch to include/exclude recursive annotated variations (defaults to 'no'.)

    • nags -- switch to include/exclude numberic annotation glyphs (defaults to 'no'.)

    • ECO -- switch to include/exclude ECO tag (defaults to 'yes'.)

    • NIC -- switch to include/exclude NIC tag (defaults to 'no'.)

    • Opening -- switch to include/exclude Opening tag (defaults to 'yes'.)

    • exclude -- an array reference of tags to be excluded (defaults to undef.)

      This is an array reference where the referent has the form of (tag_to_exclude_1..tag_to_exclude_n), i.e.:

       my @exclude = qw(

      again, as used in the SYNOPSIS example, this would eliminate the 'WhiteElo', 'BlackElo' and 'EventDate' tags from the .pgn file being processed.

    • sticky -- switch to turn on/off 'sticky' nature of the data in the 'Event', 'Site' and 'Date' tags (defaults to 'yes'.) Essentially this allows a tag to remember and use the previous games tag if the tag contents for current game is either '?' or empty.

    • autoround -- switch to turn on/off autoincrement for the 'Round' tag (default is 'yes'.) Similar to 'sticky', if a 'Round' tag is either empty or set to '?' then the current tag is set to the value of the previous tag plus one.

  • keys for filtertype XML. These control the appearence of embedded positions reached during the game as well as the final position of the game.

    • fen -- switch to include/exclude fen information for each move (defaults to 'no'.)

    • position -- switch to control position diagrams in a game (defaults to 'yes'.)

      Possible values are:

      • 'nag' -- insert diagram for each {0} in game text.

      • 'end' -- insert diagram at end of game.

      • 'no' -- no diagrams from either source.

      • 'yes' -- create diagrams based on both embedded nags as well as at end of game.

    • font -- name of font to specify for embedded diagrams (default is 'Chess Kingdom'.)

      Following list shows font name, font designer. They are available from

      • Chess Cases -- Matthieu Leschemelle

      • Chess Adventurer -- Armando H. Marroquin

      • Chess Alfonso-X -- Armando H. Marroquin

      • Chess Alpha -- Eric Bentzen

      • Chess Berlin -- Eric Bentzen

      • Chess Condal -- Armando H. Marroquin

      • Chess Harlequin -- Armando H. Marroquin

      • Chess Kingdom -- Armando H. Marroquin

      • Chess Leipzig -- Armando H. Marroquin

      • Chess Line -- Armando H. Marroquin

      • Chess Lucena -- Armando H. Marroquin

      • Chess Magnetic -- Armando H. Marroquin

      • Chess Mark -- Armando H. Marroquin

      • Chess Marroquin -- Armando H. Marroquin

      • Chess Maya -- Armando H. Marroquin

      • Chess Mediaeval -- Armando H. Marroquin

      • Chess Mérida -- Armando H. Marroquin

      • Chess Millennia -- Armando H. Marroquin

      • Chess Miscel -- Armando H. Marroquin

      • Chess Montreal -- Gary Katch

      • Chess Motif -- Armando H. Marroquin

      • Chess Plain -- Alan Hickey

      • Chess Regular -- Alistair Scott

      • Chess Usual -- Armando H. Marroquin

      • Chess Utrecht -- Hans Bodlaender

      • Tilburg -- Eric Schiller and Bill Cone

      • Traveller Standard v3 -- Alan Cowderoy

    • border, values can be either 'single' or 'double' (default is 'single'.)

    • corner, values can be either 'square' or 'rounded' (default is 'square'.)

    • legend, values can be either 'yes' or 'no' (default is 'no'.)

    • size, value ranging from 1 to 6 that controls the size of the embedded diagram (default is 5.)

    Note -- not all fonts support all combinations of 'border', 'corner' and 'legend'. No warnings or errors will be generated by unsupported options, you get the best a font can do, no more!


filter - given a source file and specification, convert to supported output. See details in Description.


use Chess::PGN::Parse;
use Chess::PGN::EPD;
use Text::DelimMatch;
use Carp;
use Data::Dumper;


  • Add other output types, PDF, DHTML, LaTeX.

  • Add regular expressions to substitution mechanism.

  • Allow for 'extra' and 'inter' semicolon comments.


None known; Unknown? Of course, though I try to be neat...


Hugh S. Myers


1 POD Error

The following errors were encountered while parsing the POD:

Around line 929:

Non-ASCII character seen before =encoding in 'Mérida'. Assuming CP1252