July 2018 Archives

The Future of Perl 5

No, I'm not speculating about where Perl 5 is going, but I was imagining a few minor (hah!) features that would make life much simpler.

In fact, I was imagining a minimalist OO system with optional typing.

The following is a simple LRU cache in Moose:

package Cache::LRU {
    use Moose;
    use Hash::Ordered;
    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 {
        my ( $self, $key, $value ) = @_;
        if ( $self->_cache->exists($key) ) {
            # delete the key and set it later to ensure it's the
            # most recently used
        elsif ( $self->_cache->keys >= $self->max_size ) {
            # key doesn't exists, so make room for it
        $self->_cache->set( $key, $value );

    sub get {
       my ( $self, $key ) = @_;


And to use it:

my $cache = Cache::LRU->new( max_size => 100 );

The set($key,$value) method removes the oldest value if the cache size is greater than the max size. Then we delete our key and set it again (puts it at the top of the stack). Setting the max_size to zero disables the cache.

Here's the same code in my imagined future of Perl 5, with a class keyword. It also has method, has, and self (unshown) keywords, but only in the class body. It also has private and public attributes, and optional typing.

class Cache::LRU {
    use Hash::Ordered;
    my       $x        = Hash::Ordered->new; # private
    has UInt $max_size = 20;                 # default

    method set ( $key, $value ) {
        if ( $x->exists($key) ) {
        elsif ( $x->keys > $max_size ) {
        $x->set( $key, $value );
    method get ($key) { $x->get($key) }

The devil, of course, exists in the details, but it's so much simpler and easier to read. The bigger the class, the easier it is to read. Whether that could ever be implemented is a different story, particularly having lexically scoped keywords (e.g., keywords that only exist in a scope they'd be useful in).

What would you like to see in the future of Perl 5? And what mistakes did I make in the above?

About Ovid

user-pic Freelance Perl/Testing/Agile consultant and trainer. See http://www.allaroundtheworld.fr/ for our services. If you have a problem with Perl, we will solve it for you. And don't forget to buy my book! http://www.amazon.com/Beginning-Perl-Curtis-Poe/dp/1118013840/