It may be advantageous to modify pin() to take the length as a parameter (you could build in a default length of 4). This would allow you to write tests for the sub that require no knowledge of the implementation, and (to quote BrowserUK) "a modification by someone not understanding the significance of "%04d"" would not be (as much of) a risk because the length would not be hard-coded.

Taking this one step further, pin() could also take the character set to be used for PIN generation (0..9 could be used as a default).

While these ideas may be overkill for your current application, a generalized solution might be a nice snippet that could be reused elsewhere. It would even come with a test suite. :-)


In reply to Re: Testing Random Code by bobf
in thread Testing Random Code by Ovid

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.