http://qs1969.pair.com?node_id=369015


in reply to Re: Surviving 'Illegal division by zero'
in thread Surviving 'Illegal division by zero'

++ Thanks, that's a really interesting answer, though I think a simple function (see above) might be easier for whoever maintains my library to understand at first glance ;). It's also a shame that it isn't possible to directly re-open the method definitions of '/' and '+' for numeric scalars in Perl.

As an aside, does this solution using numeric classes make anyone else pine for them in Perl (Float, Integer, Complex ...)?

Replies are listed 'Best First'.
Re^3: Surviving 'Illegal division by zero'
by dragonchild (Archbishop) on Jun 23, 2004 at 14:36 UTC
    This will make your code easier to understand. And, this is the way to "directly re-open the method definitions". In fact, it's safer to do it this way than it is to redefine it for the whole program. That kind of "action-at-a-distance" is the source of more maintenance nightmares than anything else.

    Another way to look at it is that you can now parse, format, and handle anything to do with numbers in one place. If you want to change how you deal with numeric values, simply change the class. There's your global change, but it's nicely encapsulated.

    A last concept to leave you with - if someone were to take your code and wrap it in something else, which is the politer way to handle things?

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      And, this is the way to "directly re-open the method definitions". In fact, it's safer to do it this way than it is to redefine it for the whole program. That kind of "action-at-a-distance" is the source of more maintenance nightmares than anything else.

      Totally agree. Before Abigail's post I hadn't really thought of it in OO (obj-oriented and op-overloading) terms. To put what you're saying in, uh, ahem, pseudocode, it's the difference between globally redefining a core method

      class Float alias :old_divide :/ def / (other) other == 0 ? nil : old_divide(other) end end

      and subclassing, which is all good

      class MyFloat < Float def MyFloat.new(from) from.to_f() end def / (other) other == 0 ? nil : super(other) end end MyFloat.new(4) / 0 # nil
      A last concept to leave you with - if someone were to take your code and wrap it in something else, which is the politer way to handle things?

      Sure, it's a library. I was wondering if there was something scoped lexically analogous to:

      use warnings; { no warnings qw/once/; $foo = 5 - $bar; } { no warnings qw/uninitialized/; $foo = 5 - $qux; }

      Thanks

      I agree, especially from the viewpoint of maintainability. Better to handle such situations (there may be cases other than division by zero which need special attention) in one encapsulated place. The other choices are to dig through all the code, restructuring where needed to handle the exceptional case, or to try to capture the exception (which tells you when the problem has happened rather than letting you avoid it).