Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

eval not working the way I expected?

by convenientstore (Pilgrim)
on Jan 09, 2008 at 16:55 UTC ( #661408=perlquestion: print w/replies, xml ) Need Help??
convenientstore has asked for the wisdom of the Perl Monks concerning the following question:

I don't know what happened but I thought i posted this while ago but didn't see it on newest nodes nor under nodes I wrote so hopefulli this is not a double post

I dont' think I am understanding eval correctly or using it incorrectly but I thought below statement should catch wrong math(like division by 0 or something)

I am getting below error while running the program

} else { $oo_o{$_} = sprintf("%.2f" ,eval {( ( $start_o{$_} / ($tota +l_o{$_} - $now_o{$_}) ) * 100 )} ); $oo_xxx_o{$_} = sprintf("%.2f", eval{( ( $start_o{$_} / $to +tal_o{$_}) * 100 )} ); } } There were 5 of them occured Use of uninitialized value in subtraction (-) at ./sreport line 257, < +> line 22420 (#1) (W uninitialized) An undefined value was used as if it were alread +y defined. It was interpreted as a "" or a 0, but maybe it was a mi +stake. To suppress this warning assign a defined value to your variables. To help you figure out what was undefined, perl tells you what ope +ration you used the undefined value in. Note, however, that perl optimiz +es your program and the operation displayed in the warning may not necessa +rily appear literally in your program. For example, "that $foo" is usually optimized into "that " . $foo, and the warning will refer +to the concatenation (.) operator, even though there is no . in your program. Use of uninitialized value in division (/) at ./sreport line 257, <> l +ine 22420 (#1) Use of uninitialized value in division (/) at ./sreport line 258, <> l +ine 22420 (#1)

Replies are listed 'Best First'.
Re: eval not working the way I expected?
by friedo (Prior) on Jan 09, 2008 at 17:07 UTC
    Block eval is for catching fatal errors, so you can deal with them instead of having your program die. What you've got there is a bunch of warnings. Warnings, by definition, are not fatal and won't interrupt the flow of your program. You've got the diagnostic text right there which tells you in great detail what's wrong and how to fix it. (Hint: %start_o and %total_o don't contain what you think they do. Try using Data::Dumper to inspect them.)
Re: eval not working the way I expected?
by Errto (Vicar) on Jan 09, 2008 at 19:08 UTC
    One other thing - if the code inside the eval does produce a fatal error (and divide by zero is the only one I can think of unless the values are actually objects of a class that overrides arithmetic) you'll still get a warning printed for Use of uninitialized value in sprintf. The reason is that if the eval fails, it will return an undefined value. sprintf will coerce this to the number zero, but it will issue a warning in doing so. To avoid that, you can either disable warnings on the relevant block by saying no warnings; or you can do the conversion explicitly by saying something like eval { ... } || 0
Re: eval not working the way I expected?
by ysth (Canon) on Jan 09, 2008 at 19:32 UTC
      try few of that and get back to you guys
      thanks as always
        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 );

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://661408]
Approved by bingos
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2018-06-20 20:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (117 votes). Check out past polls.

    Notices?