use strict; use XML::Parser; my $parser = XML::Parser->new(Style => 'Stream', Handlers => {Init => \&init, Start => \&start, Char => \&char, End => \&end, Final => \&final}); $parser->parsefile(shift); #################################### # These variables keep track # # of each distribution data, and # # are reset at each distribution. # # # my $dist_count = 0; # my $dist_cost = 0; # my $dist_out = 0; # #################################### #################################### # These variables keep track # # of the totals which are # # accumulated throughout the # # parsing process # # # my $total_count = 0; # my $total_cost = 0; # my $total_out = 0; # #################################### my $curr_val = ""; ## Retains the text value ## within the current node my @os_avail = (); ## An array of available ## operating systems sub init { my $e = shift; print "\n***** Software Inventory Report *****\n"; print "-------------------------------------\n\n"; } sub start{ my ($e, $tag, %attr) = @_; if ($tag eq "distribution") { print "*$attr{name} (version $attr{version})*\n"; } elsif ($tag eq "os") { push(@os_avail,$attr{name}); } } sub char { my ($e, $string) = @_; $curr_val = $string; } sub end { my ($e, $tag) = @_; if ($tag eq "price") { $dist_cost = $curr_val; $total_cost += $curr_val; } elsif ($tag eq "total") { $dist_count += $curr_val; $total_count += $curr_val; } elsif ($tag eq "out") { $dist_out += $curr_val; $total_out += $curr_val; } elsif ($tag eq "distribution") { print "Packages: $dist_count\n"; print "Available: ".($dist_count - $dist_out)."\n"; print "Value ($dist_cost per piece): \$".($dist_count*$dist_cost)."\n"; print "-------------------------------------\n\n"; ## Empty the distribution variables $dist_count = 0; $dist_out = 0; @os_avail = (); } } sub final { my $e = shift; print "Total software packages: $total_count\n"; print "Total software packages available: ".($total_count -$total_out)."\n"; print "Total cost for $total_count pieces of software: $total_cost\n"; }