ultranerds has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to work out why my currency converter decided its gonna stop working. To test, I've made a copy of the function into just a simple test.cgi script:
#!/usr/local/bin/perl use utf8; use Locale::Currency; use File::Basename; use Cache::FileCache; use Finance::Quote; use POSIX; print "FOO: ". get_price("CHF","EUR","50") . "\n"; sub get_price { my ($currency_from,$currency_to,$amount) = @_; my ($filename) = fileparse($0, '.pl'); my $cache = Cache::FileCache->new({ cache_root => "$ENV{HOME}/.$filename", default_expires_in => '1 day', }); my $quote = Finance::Quote->new(); my $ratio = $cache->get("$currency_from:$currency_to"); $ratio = $quote->currency($currency_from, $currency_to) unless defined $ratio; die "sorry, cannot convert from $currency_from to $currency_to\n" unless defined $ratio; $cache->set("$currency_from:$currency_to", $ratio); return $amount * $ratio }
However, when I run it - I now get:
-bash-3.2$ perl test.cgi [Sat Jan 18 13:09:21 2014] test.cgi: Illegal division by zero at /usr/ +lib/perl5/site_perl/5.8.8/Finance/Quote/IndiaMutual.pm line 27. [Sat Jan 18 13:09:21 2014] test.cgi: Compilation failed in require at +(eval 34) line 1. [Sat Jan 18 13:09:21 2014] test.cgi: BEGIN failed--compilation aborted + at (eval 34) line 1. [Sat Jan 18 13:09:21 2014] test.cgi: at test.cgi line 45


I'm at a bit of a loss as to whats going on - as it was working fine before :/

TIA

Andy

Replies are listed 'Best First'.
Re: Finance::Quote giving weird error
by ww (Archbishop) on Jan 18, 2014 at 13:07 UTC
    Awright.... ;-)

    Just hadda' check, following my own suggestion above. Installed requisite modules, tweaked code and came up with this:

    #!/usr/local/bin/perl use 5.016; use warnings; # 1071094 use utf8; use Locale::Currency; use File::Basename; use Cache::FileCache; use Finance::Quote; use POSIX; say "FOO: ". get_price("CHF","EUR","50") . "\n"; sub get_price { my ($currency_from,$currency_to,$amount) = @_; my ($filename) = fileparse($0, '.pl'); my $cache = Cache::FileCache->new({ cache_root => "$ENV{HOME}/.$filename", default_expires_in => '1 day', }); my $quote = Finance::Quote->new(); my $ratio = $cache->get("$currency_from:$currency_to"); say "\$ratio: $ratio \t \$currency_from $currency_from \t \$currency_t +o: $currency_to"; $ratio = $quote->currency($currency_from, $currency_to) unless defined $ratio; say "\$ratio: $ratio, \t \$currency_from $currency_from, \t \$currency +_to: $currency_to"; die "sorry, cannot convert from $currency_from to $currency_to\n" unless defined $ratio; $cache->set("$currency_from:$currency_to", $ratio); return $amount * $ratio }
    Execution:
    C:\> 1071094.pl Use of uninitialized value $ENV{"HOME"} in concatenation (.) or string + at D:\_Perl_\PMonks\1071094.pl line 19. $ratio: 0.8113 $currency_from CHF $currency_to: EUR $ratio: 0.8113, $currency_from CHF, $currency_to: EUR FOO: 40.565

    The 'uninitialized' isn't exactly surprising; nor does it appear to be crucial. The rest is plausible, but of undetermined correct-ness.

    If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.
      Thanks for the reply :) I think I was actually just being a dumb arse!
      [Sat Jan 18 16:11:05 2014] test.cgi: Illegal division by zero at /usr/ +lib/perl5/site_perl/5.8.8/Finance/Quote/IndiaMutual.pm line 27. [Sat Jan 18 16:11:05 2014] test.cgi: Compilation failed in require at +(eval 34) line 1. [Sat Jan 18 16:11:05 2014] test.cgi: BEGIN failed--compilation aborted + at (eval 34) line 1. [Sat Jan 18 16:11:05 2014] test.cgi: at test.cgi line 45 FOO: 40.565
      The "FOO" bit with the price prints out just fine... so as you said, the debugging coming out in the SSH terminal shouldn't affect it . So in fact, it was actually running just fine... doh!
Re: Finance::Quote giving weird error
by ww (Archbishop) on Jan 18, 2014 at 12:41 UTC

    The "50" in Ln 11 doesn't match anything I found with a quick scan of the POD for Finance::Quote. Is it an attempt to use eps or another of the module's allowed numeric arguments? If so, double-check syntax. Added: appears to be the value of $amount

    And then (paraphrased Basic Debugging tip)...Try inserting print statements at Lns 27 and 30 (i.e. replace blank lines to keep Ln numbers aligned with your post) to see what's really happening:

    print "\$ratio: $ratio, \t \$currency_from $currency_from, \t \$currency_to: $currency_to" You just may find the results helpful.
    Come, let us reason together: Spirit of the Monastery