Catching nice exceptions from Moose

Moose is great.

Before Moose, creating objects in Perl was a hassle; you had to write your own constructor, and methods for accessors and mutators. At the same time, you had to try to provide a clean interface and ensure encapsulation. It was tedious. With Moose, you can build your classes without having to include lots of cruft, leaving you to do the interesting stuff.

One advantage of Moose is the ability to mark a class’s attributes as required, and apply typed constraints to those attributes. This means if you try to set an object’s attribute incorrectly, an exception is raised. Nice! However, a lot of developers new to Moose are soon daunted by the truckload of output that is thrown from Moose code. Here’s an example:

Attribute (bar) is required at /home/pete/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/x86_64-linux/Moose/Meta/ line 510
    Moose::Meta::Attribute::initialize_instance_slot('Moose::Meta::Attribute=HASH(0x1e0e878)', 'Moose::Meta::Instance=HASH(0x1e14ec8)', 'Foo=HASH(0x1429b78)', 'HASH(0x1429170)') called at /home/pete/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/x86_64-linux/Class/MOP/ line 524
    Class::MOP::Class::_construct_instance('Moose::Meta::Class=HASH(0x1daf6e0)', 'HASH(0x1429170)') called at /home/pete/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/x86_64-linux/Class/MOP/ line 497
    Class::MOP::Class::new_object('Moose::Meta::Class=HASH(0x1daf6e0)', 'HASH(0x1429170)') called at /home/pete/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/x86_64-linux/Moose/Meta/ line 274
    Moose::Meta::Class::new_object('Moose::Meta::Class=HASH(0x1daf6e0)', 'HASH(0x1429170)') called at /home/pete/perl5/perlbrew/perls/perl-5.14.1/lib/site_perl/5.14.1/x86_64-linux/Moose/ line 28
    Moose::Object::new('Foo') called at /home/pete/perl_scratch/ line 13

Continue reading