in reply to XML::parser question

Your code, changed to print all the say text so far every time a val1 attribute with the value FOO is encountered:
#!perlenv -w use strict; use XML::Parser; my $in_say = 0; my $say = ''; my $xp = new XML::Parser( Handlers => { Start => \&start_handler, End => \&end_handler, Char => \&char_handler } ); if ( $#ARGV < 0 ) { print "usage: blah <xml file>"; exit; } $xp->parsefile( $ARGV[0] ); sub start_handler { my ( undef, $elem, %attrs ) = @_; if ( $elem eq 'say' ) { $in_say = 1; } my $val1 = $attrs{val1}; if (defined $val1 && $val1 eq 'FOO') { print $say; } } sub end_handler { my ( undef, $elem ) = @_; if ( $elem eq 'say' ) { $in_say = 0; } } sub char_handler { my ( undef, $str ) = @_; if ($in_say) { $say .= $str; } }
Also changed: The global $xp is not hidden by a local (my) $xp in every handler.