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
            $self->_cache->delete($key);
        }
        elsif ( $self->_cache->keys >= $self->max_size ) {
            # key doesn't exists, so make room for it
            $self->_cache->shift;
        }
        $self->_cache->set( $key, $value );
    }

    sub get {
       my ( $self, $key ) = @_;
       $self->_cache->get($key)
    }

    __PACKAGE__->meta->make_immutable;
}

And to use it:

my $cache = Cache::LRU->new( max_size => 100 );
$cache->set($key,$value);
$cache->get($key);

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) ) {
            $x->delete($key);
        }
        elsif ( $x->keys > $max_size ) {
            $x->shift;
        }
        $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?

Tau Station is now live!

It's been a few years in the making, but Tau Station is now live!

It's a free-to-play post-apocalyptic interstellar MMORPG that runs in a browser, tablet, or mobile. The backend is written in Perl.

Join Tau Station and let's show the world the awesome things you can do with Perl! (And hey, spend money if you can; I need to keep the lights on) :)

We follow WCAG 2.0 AA standards for accessibility (blind and mobility impaired people can play).

Tau Station is now live

Building a Software Consulting Firm

Hey, I'm not dead! I just haven't posted in a while because I've been so busy on Tau Station (which, if all goes well, will be open for everyone real soon™).

In the meantime, I've written a bit about building a software consulting firm. You might find it interesting.

If you need to outsource software development with a firm you can trust, contact us.

Merry Christmas from Tau Station!

I know some people have waited a long time for this. Here's a quick blog post about how to sign up for a chance to participate in the closed alpha.

Why I wrote Keyword::DEVELOPMENT

I've had a few discussions with people about why I wrote Keyword::DEVELOPMENT, a useful, but simple module. In short, it lets you do this:

use Keyword::DEVELOPMENT;

# later ...

sub consume_item ( $self, $item_slug ) {
    DEVELOPMENT {
      # expensive and time-consuming debugging code here
    }
    my $inventory = $self->inventory;
    return $self->new_exchange(
        slug => 'consume',
        Steps(
            Inventory(  $inventory => contains => $item           ),
            Consumable( $self      => consume  => 'item_instance' ),
            Inventory(  $inventory => remove   => 'item_instance' ),
        ),
    )->attempt;
}

The expensive debug block? With Keyword::DEVELOPMENT, is simply fails to exist in production unless the PERL_KEYWORD_DEVELOPMENT environment variable has a true value. Thus, there is no performance overhead to instrumenting your code with complicated debugging logic (as I'm currently doing with Tau Station).

It's been pointed out to me by several people that I can accomplish the above in regular Perl with constant folding. I'll explain what that means and why I prefer the keyword route.

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/