Object::Util

I've recently released a new module called Object::Util to CPAN. This is a set of handy methods for working with classes and objects in Perl. It is inspired by Safe::Isa, a module which takes advantage of the fact that coderefs may be called as methods on Perl objects:

   my $method = sub { ... };
   $object->$method(@args);

Object::Util gives you a set of coderef utility methods that are applicable to a wide range of classes and objects.

Planet Moose - August 2014

Welcome to Planet Moose, a brief write up on what's been happening in the world of Moose in the past month, for the benefit of those of you who don't have their eyes permanently glued to the #moose IRC channel, or the MetaCPAN recent uploads page.

It's now a year since the first Planet Moose. I'm surprised I've managed to keep it up for a whole twelve months. (Though it's not always been posted promptly on the first day of the month!)

If you'd like to contribute some news for next month's issue, you can do so on the wiki.

Moose

Moose 2.1211 has been released containing some documentation updates and minor changes to Moose::Exporter.

More interestingly, three Moose 2.13xx trial releases have been uploaded to CPAN. The major new feature so far is improved support for overloaded operators, especially in roles, eliminating the need for MooseX::Role::WithOverloading.

Data::Dumper Debugging

I've never really used the Perl debugger much (maybe I should learn?) and usually resort to lots of use Data::Dumper; print Dumper($somevar); statements to help me understand what's going wrong with a piece of code.

Type::Tiny Tricks #7: Tricks with Dicts

In Perl, hashes are typically used for two sorts of purposes: maps (where the hash key acts as an object identifier) and dictionaries (where the hash key acts like a field name). A quick illustration of what I mean by this:

   # Maps
   my %ages = (
      alice   => 24,
      bob     => 25,
      carol   => 31,
   );
   my %email = (
      alice   => "alice@example.net",
      bob     => "robert.smith@example.com",
      carol   => "c_jones@example.org",
   );
   
   # Dictionaries
   my %alice = ( age => 24, email => "alice@example.net" );
   my %bob   = ( age => 25, email => "robert.smith@example.com" );
   my %carol = ( age => 32, email => "c_jones@example.org" );

These are two different styles of using hashes. Sometimes one is useful, and sometimes the other is. Sometimes neither is better and your choice of one over the other is fairly arbitrary.

Types::Standard contains a type constraint called Map which is useful for validating references to the first kind of hash. (I copied the idea from MooseX::Types::Structured.) But today I'm mainly going to talk about another type constraint: Dict. (Yeah, I copied that one too.)

Type::Tiny Tricks #6: Tricks with Tuples

Let's say you want an attribute to accept a pair of numbers - perhaps a geographic co-ordinates [ 50.873, -0.002 ]. You could constrain the attribute as ArrayRef[Num], but that would accept an arrayref containing a single number, or eight numbers, or even a reference to an empty array.

With the Tuple type constraint, you can be more exact in expressing which values are acceptable:

   isa  => Tuple[ Num, Num ]