Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Math::Round::Var::round() takes no action when a number is not supplied

by metaperl (Curate)
on Aug 30, 2011 at 16:48 UTC ( #923265=perlquestion: print w/ replies, xml ) Need Help??
metaperl has asked for the wisdom of the Perl Monks concerning the following question:

Math::Round::Var has the following function:
###################################################################### +## =head2 round $number = $rounder->round($number); =cut sub round { my $self = shift; my $rnd = $self->{precision}; my $number = shift; return(sprintf("%0.${rnd}f", $number)); } # end subroutine round definition ###################################################################### +##
Please notice that there is neither a warning or exception if a number is not supplied. Is lack of a number argument an exception? Or should it throw a warning? Or is everything OK as it is?

Comment on Math::Round::Var::round() takes no action when a number is not supplied
Download Code
Replies are listed 'Best First'.
Re: Math::Round::Var::round() takes no action when a number is not supplied
by toolic (Bishop) on Aug 30, 2011 at 17:04 UTC
    Did you try it? I get a warning:
    use Math::Round::Var; my $rnd = Math::Round::Var->new(0.01); # rounds to two decimal places: print $rnd->round(), "\n"; __END__ Use of uninitialized value in sprintf at .../Math/Round/Var.pm line 22 +9. 0.00
    The module's source code uses warnings.
      Did you try it?
      I shore did.
      I get a warning
      That warning came from Perl - the subroutine simply assumed you wanted to round the number zero - an undefined value was coerced to zero in context.
Re: Math::Round::Var::round() takes no action when a number is not supplied
by BrowserUk (Pope) on Aug 30, 2011 at 17:04 UTC

    It seems to raise a perfectly valid exception for me:

    sub round { my $self = shift; my $rnd = $self->{precision}; my $number = shift; return(sprintf("%0.${rnd}f", $number)); };; print round( {precision=>3}, 1.23456 );; 1.235 print round( {precision=>3}, 1 );; 1.000 print round( {precision=>3} );; Use of uninitialized value $number in sprintf at ... 0.000

    (Though quite why you'd use a module for this is beyond me?)


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      It seems to raise a perfectly valid exception for me
      Well, a Perl-generated warning is not the same as a program-generated exception, and here we have only a Perl-generated warning.
        a Perl-generated warning is not the same as a program-generated exception,

        Really? So how do you handle them differently?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://923265]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (16)
As of 2015-07-31 21:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (282 votes), past polls