Two more tips to make your Perl more robust:
$ cat math.pm package math; use strict; use warnings; # Perl Cookbook, 2.1. Checking Whether a String Is a Valid Number # Return 1 if input is a C float, otherwise undef. sub is_float { ($_[0] && $_[0] =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$ +/) ? return 1 : return; } sub addition { my $sum; my (@mynumbers) = @_; for (@mynumbers) { die "invalid argument" if !is_float($_); } for (@mynumbers) { $sum += $_; } return $sum; } 1; __END__ $ cat math.t use strict; use warnings; use Test::More qw(no_plan); use Test::Exception; use lib qw(.); BEGIN { use_ok('math'); } # ------ some negative tests throws_ok { math::addition(undef); } qr/invalid argument/, q{caught invalid argument ok}; throws_ok { math::addition(q{a}); } qr/invalid argument/, q{caught invalid argument ok}; throws_ok { math::addition(1, 2, 3, q{-}); } qr/invalid argument/, q{caught invalid argument ok}; # ------ some positive tests cmp_ok(math::addition(1, 2, 3), q{==}, 6, q{Returns 6}); ok(math::is_float(math::addition(1e2, 2.5, 3.14)), q{Returns a float}) +; cmp_ok(math::addition(1.5, 2.5, 3.5), q{==}, 7.5, q{Returns 7.5}); __END__ $ prove math.t math....ok All tests successful. Files=1, Tests=7, 0 wallclock secs ( 0.04 cusr + 0.01 csys = 0.05 C +PU)
--
Andreas

In reply to Re: cheesy comparison program not working!! by andreas1234567
in thread cheesy comparison program not working!! by navinjathan

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.