use XML::Parser; my $file = shift; my $parser = new XML::Parser(Style=>'Subs', Pkg=>'SubHandlers', ErrorContext => 2); $parser->setHandlers(Char => \&char_handler); $parser->parsefile($file); # --------------------------------------------- sub char_handler { my ($p, $data) = @_; if ($::state::store_contents) { $::state::storage{$::state::store_contents} .= $data; } else { # print $data; } } # --------------------------------------------- package SubHandlers; # # This function would perform the given action (eg. buy or sell) # on the given stock. In this case it's simply a stub. sub take_action { my ($symbol, $action) = @_; print "Taking action \"$action\" on stock \"$symbol\" .\n"; } sub stock_quotes { # Open connection to database use DBI; $dsn = "DBI:mysql:database=test;"; $::state::dbh = DBI->connect($dsn); } sub stock_quotes_ { # Close connection to database $::state::dbh->disconnect(); } sub stock_quote { undef %::state::storage; } sub stock_quote_ { # The values have been stored in the hash %state::storage my $symbol = $::state::storage{'symbol'}; # Grab the rules for the given stock from the rules table my $sth = $::state::dbh->prepare("select * from rules where symbol='$symbol'"); $sth->execute(); while (my $ref = $sth->fetchrow_hashref()) { my $field = $ref->{'field'}; my $value = $ref->{'value'}; if ($::state::storage{$field} > $::state::storage{$value}) { # This rule applies print "Rule \"$field > $value\" applies for $symbol\n"; take_action($symbol, $ref->{'action'}); } } $sth->finish(); undef %::state::storage; } sub symbol { $::state::store_contents = "symbol"; } sub symbol_ { undef $::state::store_contents; } sub date { $::state::store_contents = "date"; } sub date_ { undef $::state::store_contents; } sub time { $::state::store_contents = "time"; } sub time_ { undef $::state::store_contents; } sub price { my $expat = shift; my $element = shift; # Read the attributes while (@_) { my $att = shift; my $val = shift; $attr{$att} = $val; } if ($attr{'type'} eq "ask") { $::state::storage{'price'} = $attr{'value'}; } } sub price_ { } sub volume { $::state::store_contents = "volume"; } sub volume_ { undef $::state::store_contents; }