Testing is really super important in a shop that is serious and trying to not waste anyone's time or accidentally ship stuff sideways to customers. Even in the shortest code or whimsical examples like yours, bugs creep in. Proper tests help ensure they don't go unnoticed.

Perl Testing: A Developer's Notebook (ISBN 0596100922) is really quite good. Pick it up. Make your own big projects better and wow the next interview committee. Other required reading: prove, Test::Simple, Test::More, App::Prove. Plenty of other Test:: space to explore as you like.

Here are your tests rewritten (style differences are up to you, I like to omit parens and explicit returns where possible).

use strictures; use Test::More; # plan test count here or use done_testing($count). is hello_world(), "Hello world!", "Hello world is hello world"; # Naive test, will not catch your bug, and you have one. cmp_ok get_number(), ">", 0, "get_number() returns greater than zero"; subtest "Iterate on get_number()" => sub { my $iterations = 10_000; # Higher confidence. my $ok; for ( 1 .. $iterations ) { $ok++ if get_number() > 0; } my $msg = "get_number() returns greater than zero"; $ok == $iterations ? pass($msg) : fail($msg . " - expected $iterations > 0, got $ok"); done_testing(1); }; done_testing(3); # <- Either "plan" or delcare test count here! exit 0; sub hello_world { "Hello world!"; } sub get_number { int(rand(1000)); }
~>prove my-test.t -v ok 1 - Hello world is hello world ok 2 - get_number() returns greater than zero not ok 1 - get_number() returns greater than zero - expected 10000 + > 0, got 9993 1..1 not ok 3 - Iterate on get_number() 1..3 Dubious, test returned 1 (wstat 256, 0x100) Failed 1/3 subtests Test Summary Report ------------------- /home/moo/my-test.t (Wstat: 256 Tests: 3 Failed: 1) Failed test: 3 Files=1, Tests=3, 0 wallclock secs ( 0.03 usr 0.01 sys + 0.03 cusr + 0.00 csys = 0.07 CPU) Result: FAIL

Found your bug, right? int(rand(whatever)) can return zero. Fix with int(rand(1000)+1) and run again.

ok 1 - Hello world is hello world ok 2 - get_number() returns greater than zero ok 1 - get_number() returns greater than zero 1..1 ok 3 - Iterate on get_number() 1..3 ok All tests successful. Files=1, Tests=3, 0 wallclock secs ( 0.03 usr 0.01 sys + 0.03 cusr + 0.00 csys = 0.07 CPU) Result: PASS

In reply to Re: why Test::More? by Your Mother
in thread why Test::More? by fionbarr

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.