Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Recap: The Future of Perl 5

by tobyink (Canon)
on Aug 23, 2018 at 18:18 UTC ( [id://1220943]=note: print w/replies, xml ) Need Help??


in reply to Recap: The Future of Perl 5

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) ) +; } }

Replies are listed 'Best First'.
Re^2: Recap: The Future of Perl 5
by Ovid (Cardinal) on Aug 23, 2018 at 19:32 UTC

    ++ from me and I agree with all of your points (except I didn't make it deliberately verbose because it was thrown together quickly; my bad).

    However, Mew, like Moose, or Moo, is not core. Not having a clean, minimal syntax for core OO is part of the problem I would love to see us address (and obviously, I mean more than bless and @ISA).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-03-29 12:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found