Untested, for illustration only.
sub new { my $Di = { Sides => 6, Face => 1 }; #default six sider, sitting with 1 up bless $Di, "Dice::Di"; } sub Roll { my $self = shift; my $newface = int( rand( $self->{'Sides'} ) + 1); $self->{'Face'} = $newface; return $newface; } sub Rolls { #call with @results = $Di->Rolls( integer ); my $self = shift; my $times = shift; my @faces; for (1..$times) { my $rolled = $self->Roll(); push( @faces, $rolled ); } return @faces; } sub CurrentFace { my $self = shift; return $self->{'Face'}; }
If you really want to be able to know every roll this particular Di has ever yielded you could add History => (), to the new method and push $newface onto that during Roll(). I would avoid this unless it were heavily restricted in the implementation. Also, using the wantarray function (or even detecting the presence of an argument) you can easily have the Roll() method detect whether or not it should act like the Rolls() function and return a list, or just roll once and return that as a scalar. Does this help?

In reply to Re: Re: (ichimunki) Re (tilly) 1: Dice::Dice by ichimunki
in thread Dice::Dice 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.