in reply to What is wrong with my calculations?
my $newrate = 0; ### AVOID ANY NASTY DIVISION-BY-ZERO ERRORS WITH AN EVAL... eval { my $newrate = ($first_currency / $second_currency); }; ### IF EVAL FAILS, LET'S PRINT THE ERROR... print $@;
You've declared a new lexical variable $newrate that only exists for the duration of the eval block (and hides the previously declared one in the enclosing scope).
You also always print $@. Rather than using eval, probably what you really wanted was:
my $newrate = 0; if( $second_currency == 0 ) { warn "Second currency was zero\n"; } else { $newrate = $first_currency / $second_currency; }
|
|---|