#!/bin/perl -w

#########################################################################
#                                                                       #
#  This example shows how to use the flush method                       #
#  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->flush;                                     # flush the end of the twig  

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

    $twig->flush;                                 # flush the twig so far   
 }

