Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^6: The future of Perl?

by morgon (Priest)
on Dec 14, 2014 at 18:44 UTC ( [id://1110325]=note: print w/replies, xml ) Need Help??


in reply to Re^5: The future of Perl?
in thread The future of Perl?

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>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1110325]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-04-23 08:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found