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";
}
####
15
8
10
2
299.99
15
8
1299.00
15
8
399.00
15
8
10
2
10
2
10
2
2999.00
####
***** Software Inventory Report *****
---------------------------------------------------
*Dreamweaver (version 4)*
Packages Purchased: 25
Packages Available: 15
Cost (299.99 per piece): $7499.75
----------------------------------------------------
*Microsoft Visual C++ Enterprise Edition (version 6)*
Packages Purchased: 15
Packages Available: 7
Cost (1299.00 per piece): $19485
----------------------------------------------------
*XML Spy (version 4.2)*
Packages Purchased: 15
Packages Available: 7
Cost (399.00 per piece): $5985
----------------------------------------------------
*Borland JBuilder Enterprise (version 6)*
Packages Purchased: 45
Packages Available: 31
Cost (2999.00 per piece): $134955
----------------------------------------------------
Total software packages purchased: 100
Total software packages available: 60
Total cost for 100 software packages: 4996.99
####
***** Software Inventory Report *****
-------------------------------------
Total software packages:
Total software packages available: 0
Total cost for pieces of software: 4996.99