I don't see the need for declaring your variables on one line and assigning to them on the next, or the if/else as you could do ...

my( $qty, $type, $face, @dice ) = ( $1, $2, $3 ? 1 : undef ); push @dice, Die->get( $type, $face ) while $qty--;

... to reduce 9 lines of code to just 2.

Granted, that destroys the value of $qty in the process, but it's easy to preserve by substituting for 1 .. $qty (as Mirod suggests) for the while $qty--.

You may also want to tighten up your regular expression (the .*-thing has been beaten to death), especially if the only optional argument following the second number is an 's'.

As an alternative to the use of a regexp to pass arguments though, I'd personally be more inclined to pass them seperately rather (ie, grab( -dice => 3, -sides => 6, -face => 0 ); or even grab( qw( 3 d 6 s ) );), possibly coupling them with defaults if they aren't set.

    --k.



In reply to Re: BWTDI: Better Way To Do It? by Kanji
in thread BWTDI: Better Way To Do It? by coreolyn

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.