########################################### package Plugger::Position; # 2004, Mike Schilli ########################################### use strict; use warnings; use Log::Log4perl qw(:easy); use Finance::YahooQuote; use Term::ANSIColor; ########################################### sub init { ########################################### my($class, $ctx) = @_; DEBUG "Registering @_"; $ctx->register_cmd("stock", undef, \&process, undef); $ctx->register_cmd("cash", undef, \&process_cash, undef); } ########################################### sub process { ########################################### my($ctx, $cmd, @args) = @_; my $value = price($args[0]) * $args[1]; my $gain = $value - $args[2] * $args[1]; Plugger::Account::position( ucfirst($cmd), @args[0..2], price($args[0]), $value, $gain); my $mem = $ctx->mem(); $mem->{account_subtotal} += $value; $mem->{account_total} += $value; } ########################################### sub process_cash { ########################################### my($ctx, $cmd, @args) = @_; my $mem = $ctx->mem(); $mem->{account_subtotal} += $args[0]; $mem->{account_total} += $args[0]; Plugger::Account::position( ucfirst($cmd), (undef) x 4, $args[0], undef); } use Cache::FileCache; my $cache = Cache::FileCache->new( { namespace => 'Dagobert', default_expires_in => 600, }); ########################################### sub price { ########################################### my($stock) = @_; DEBUG "Fetching quote for $stock"; my $cached = $cache->get($stock); if(defined $cached) { DEBUG "Using cached value: $cached"; return $cached; } my @quote = getonequote $stock; die "Cannot get quote for $stock" unless @quote; $cache->set($stock, $quote[2]); return $quote[2]; } 1;