#!/bin/perl -w

#########################################################################
#                                                                       #
#  This example shows how to use the twig_handlers option               #
#  It creates a new element named blg, for each player                  #
#                                                                       #
#########################################################################

use strict;
use XML::Twig;

my $twig= new XML::Twig( 
                twig_handlers =>                 # player will be called
                  { player => \&player }         # when each player element
                       );                        # has been parsed

$twig->parsefile( "nba.xml");    # build the twig
$twig->print;                    # print it

sub player
 { my( $twig, $player)= @_;                      # handlers params are always
                                                 # the twig and the element

   my $g  = $player->first_child( 'g')->text;    # get the text of g            
   my $blk= $player->first_child( 'blk')->text;  # get the text of blk
   my $blg= sprintf( "%2.3f", $blk/$g);          # compute blg
   my $eblg= new XML::Twig::Elt( 'blg', $blg);   # create the element
   $eblg->paste( 'last_child', $player);         # paste it in the document   
 }

