Firstly…
Class::Tiny::Antlers
Whaaaaat?!?! One of my modules is referenced by the core Perl documentation? How did that happen?!
Secondly, the Moose example could be simplified quite a bit. I feel you're making it deliberately verbose by not using sub signatures for example.
package Cache::LRU {
use Hash::Ordered;
use Moose;
use MooseX::Types::Common::Numeric qw/PositiveOrZeroInt/;
use namespace::autoclean;
has '_cache' => (
is => 'ro',
isa => 'Hash::Ordered',
default => sub { Hash::Ordered->new },
);
has 'max_size' => (
is => 'ro',
isa => PositiveOrZeroInt,
default => 20,
);
sub set ($self, $key, $value) {
if ( $self->_cache->exists($value) ) {
$self->_cache->delete($value);
}
elsif ( $self->_cache->keys > $self->max_size ) {
$self->_cache->shift;
}
$self->_cache->set( $key, $value );
}
sub get ($self, $key) {
return unless $self->_cache->exists($key);
return $self->_cache->set( $key, $self->_cache->delete($key) )
+;
}
__PACKAGE__->meta->make_immutable;
}
Still more verbose than your proposed syntax, but not as bad as it was. Also, if you used Moo, you could avoid the make_immutable stuff because the constructor automatically immutablizes the class the first time it gets called.
To be honest, Moose seems verbose because this is such a simple class. It means that boilerplate imports and stuff starts taking up a significant portion of the class. In a longer class with more methods, the difference would be a lot less pronounced.
I'mma give a Mew example because Mew is kinda cool.
package Cache::LRU {
use Hash::Ordered;
use Mew;
has _cache => InstanceOf['Hash::Ordered'], (default => sub { Has
+h::Ordered->new });
has max_size => PositiveOrZeroInt, (default => 20);
sub set ($self, $key, $value) {
if ( $self->_cache->exists($value) ) {
$self->_cache->delete($value);
}
elsif ( $self->_cache->keys > $self->max_size ) {
$self->_cache->shift;
}
$self->_cache->set( $key, $value );
}
sub get ($self, $key) {
return unless $self->_cache->exists($key);
return $self->_cache->set( $key, $self->_cache->delete($key) )
+;
}
}
-
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.