Hi

I've got a fairly chunky library that parses a big XML file full of figures. As well as grabbing the numbers out of the appropriate bits, it also calculates figures for growth percentages. Once I remembered to coerce the strings from the XML into figures (so '0.000' == 0 ), I still have to check every time before I do a division that the amount I'm dividing by isn't 0:

my $fig_1 = get_numeric_value_from_xml(...); my $fig_2 = get_numeric_value_from_xml(...); my $growth; if ( $fig1 != 0 ) { $growth = ( $fig_2 / $fig_1 * 100 ) - 100; }

If I don't check every time before I do a calculation like this, I end up dying of 'Illegal division by zero' whenever $fig_1 is zero. I was wondering if there's some way I can alter this behaviour - preferably only in the scope of this file - so that instead of dying, a division by zero simply returns undef?

TIA
Alex


In reply to Surviving 'Illegal division by zero' by ViceRaid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.