I am not really an expert on Moose but here is one attempt:
package FixedSizeQueue;
use Moose;
has "max_size" => (is => "ro", isa => "Int", default => 10);
has "items" => (is => "ro", isa => "ArrayRef", default => sub { [] });
sub pop {
my($this)=@_;
pop @{$this->items};
}
sub push {
my($this, $item)=@_;
push @{$this->items}, $item;
shift @{$this->items} if scalar @{$this->items} > $this->max_size;
}
This is fuctional but you need to be intantiate it like this:
my $q = FixedSizeQueue->new(max_size => 3);
$q->push(3);
$q->push(5);
$q->push(7);
$q->push(9);
print Dumper($q);
If you want to supply only an integer to the constructor you need to add a BUILDARGS-method to the above:
sub BUILDARGS {
my($this, $max_size)=@_;
return { max_size => $max_size };
}
Now you can use it like your Ruby-example:
my $q = FixedSizeQueue->new(3);
$q->push(3);
$q->push(5);
$q->push(7);
$q->push(9);
print Dumper($q);
</c>
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.