in reply to Re^2: eval not working the way I expected?
in thread eval not working the way I expected?

should below syntax work?
I tried it and I thought no matter what , worse thing it would do is assign 0 value to $report{$_}, but I still see error messages
$report{$_} = sprintf("%.2f", eval {( ( $stop{$_} / 60000 ) / $stop_2 +{$_})} || 0 );

Replies are listed 'Best First'.
Re^4: eval not working the way I expected?
by convenientstore (Pilgrim) on Jan 10, 2008 at 04:10 UTC
    I got rid of eval as replace it w/ below and seem to work well
    Problem now is that I cannot access the hash outside of block.. why?? meaning when I do a dump on hash,(which had element in it ; by printing inside of loop) , nothing comes up.
    my (%report_ccr,%report_asr); my (%report_ccr_o,%report_asr_o); my (%report_stop, %report_o_stop); for (sort keys %totalcalls_o) { my $start_o_f = ($start_o{$_}) ? $start_o{$_} : 0; #for stop record outbound side; if (exists $stop_outbound{$_}) { #print "\$stop_o{$_} is $stop_o{$_}\n"; #print "\$stop_outbound{$_} is $stop_outbound{$_}\n"; $report_o_stop{$_} = sprintf("%.2f", ( ( $stop_outbound{$_} + / 60000 ) / $stop_o{$_})) ; #resultsin min mark #print "\$report_o_stop{$_} is $report_o_stop{$_}\n"; } elsif (defined $crank_o{$_}) { $report_ccr_o{$_} = sprintf("%.2f" , ( ( $start_o_f / ($tota +lcalls_o{$_} - $crank_o{$_}) ) * 100 )); $report_asr_o{$_} = sprintf("%.2f" , ( ( $start_o_f / $total +calls_o{$_}) * 100 )); #print "\$report_ccr_o{$_} is $report_ccr_o{$_}\n"; #print "\$report_asr_o{$_} is $report_asr_o{$_}\n"; } else { $report_asr_o{$_} = sprintf("%.2f" , ( ( $start_o_f / $total +calls_o{$_}) * 100 )); # print "from last \$report_asr_o{$_} is $report_asr_o{$_}\n +"; } } print "going dumper\n"; print Dumper(%report_ccr_o);
Re^4: eval not working the way I expected?
by ysth (Canon) on Jan 10, 2008 at 05:04 UTC
      There was actually flow in my logic.. so I corrected and works now
      I still don't understand why eval { } || 0 did not work though
      for (sort keys %totalcalls)
      {
      
            my $start_i_f = ($start{$_}) ? $start{$_} : 0;
            $report_stop{$_} = sprintf("%.2f", ( ( $stop_inbound{$_} / 60000 ) / $stop{$_})) if $stop_inbound{$_};
            if (defined $crank{$_}) {
                $report_ccr{$_} = sprintf("%.2f" , ( ( $start_i_f / ($totalcalls{$_} - $crank{$_}) ) * 100 ));
                $report_asr{$_} = sprintf("%.2f" , ( ( $start_i_f / $totalcalls{$_}) * 100 )); 
            } else {
                $report_asr{$_} = sprintf("%.2f" , ( ( $start_i_f / $totalcalls{$_}) * 100 ));
                
            }
      }    
      
      

        I still don't understand why eval { } || 0 did not work though

        eval doesn't affect warnings. And the warning occurred during the division, not after the eval was completed.

        >perl -we"my $x; my $q = eval { $x / 4 } || 0; Use of uninitialized value in division (/) at -e line 1. >perl -we"my $x; my $q = eval { ($x||0) / 4 }; >perl -we"my $x; my $q = ( $x / 4 ) || 0; Use of uninitialized value in division (/) at -e line 1. >perl -we"my $x; my $q = ( ($x||0) / 4 );