#!/bin/perl -w

#########################################################################
#                                                                       #
#  This example updates the information for a single player             #
#  It uses the finish_print method                                      #
#                                                                       #
#########################################################################

use strict;
use XML::Twig;

my $name= shift;
my $stat_name= shift;
my $stat_value= shift;

my $twig= new XML::Twig( twig_handlers => { player => \&player } );
if( $ARGV[0]) { $twig->parsefile( $ARGV[0]); }        # parse a file
else          { $twig->parse( \*STDIN);      }        # parse the standard input
exit;

sub player
  { my( $twig, $player)= @_;
    my $player_name= $player->first_child( 'name')->text;
    if( $player_name=~ /$name/i)
      { my $stat= $player->first_child( $stat_name);
        $stat->set_text( $stat_value);
        $twig->finish_print;                               # this is it 
      }
    else
      { $twig->flush; }                                    # print players before the right one
  }
