use warnings; use strict; use XML::Parser; # See Larry Wall, Programming Perl, pp. 613-614 # This perl script parses Maple worksheets in XML format (.mw files) and # outputs only the contents of the Maple Input Text-fields (effectively # outputting a Maple program that can be stored in a .mpl file). my $filename; my $p; # The parser my @out; my $processing_input_field; # Boolean flag to indicate when we are processing a Maple Input field. sub handle_start { my ($p, $el, %atts) = @_; if ($el eq 'Input') { # Opening 'Input' $processing_input_field = 1; } elsif ($el eq 'Text-field' and $processing_input_field) { # Opening 'Text-field' setHandlers $p Char => sub { $out[-1] .= $_[1] }; } } sub handle_end { my ($p,$el) = @_; if ($el eq 'Input') { # Closing 'Input' $processing_input_field = 0; } elsif ($el eq 'Text-field' and $processing_input_field) { # Closing 'Text-field' $out[-1] .= "\n"; setHandlers $p Char => sub { }; } } $filename = $ARGV[0]; $p = new XML::Parser(Handlers => { 'Start' => \&handle_start, 'End' => \&handle_end, 'Char' => sub { } }); push @out, ""; $p->parsefile($filename); print @out, "\n";