-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 =pod It always strikes me as odd how people single out Moose as a dependency they're afraid to use or introduce into their application. One of the arguments I've heard is "I don't want to make something overly complex with dependencies that don't relate to the task". I think this view is misguided, because the alternative is to add unnecessary code and tests to your application. The best code is code that you don't have to maintain. Anyway, hopefully an example will clarify my point. Let's write a small class that has one field called "bar" that is a My::Bar object. With Moose, we just say: lang:Perl package My::Class; use Moose; use My::Bar; has 'bar' => ( is => 'rw', isa => 'My::Bar', ); Then you can say: use My::Class; my $class = My::Class->new( bar => My::Bar->new ); print $class->bar; # or whatever Let's look at the non-Moose approach: package My::Class; use strict; use warnings; use My::Bar; use Carp; use Scalar::Util qw(blessed); sub new { my ($class, %args) = @_; # crappy error message if %args is odd croak 'bar must be a My::Bar' unless blessed $args{bar} && $args{bar}->isa('My::Bar'); my $self = { bar => $args{bra} }; return bless $self => $class; } sub bar { my $self = shift; my $setting = scalar @_ > 0; my $new_bar = shift; if($setting){ croak 'bar must be a My::Bar' unless blessed $new_bar && $new_bar->isa('My::Bar') $self->{bar} = $new_bar; } return $self->{bar}; } There you have it. With 21 more lines of code, you have almost the same functionality. Oh, did you catch the typo in C? Of course not, you haven't written any of the tedious C-style tests yet. Let's write some: #!/usr/bin/env perl use Test::More plan => 'no_plan'; # I'm lazy. use Test::Exception; use ok 'My::Class'; use My::Bar; { # ideal case my $bar = My::Bar->new; my $c; lives_ok { $c = My::Class->new( bar => $bar ) }; isa_ok $c, 'My::Class', '$c'; isa_ok $c->bar, 'My::Bar', '$c->bar'; } { # not a bar my $bar = 42; throws_ok { My::Class->new( bar => $bar ) } qr/bar must be a My::Bar/; } # test for bar == ref but not blessed # test for bar blessed, but not isa My::Bar # test get # test set (like above, but for bar insead of new) So far I've written 25 lines of code just to see if C works, and it doesn't even exercise all of C! With Moose, I don't have to write these tests because I haven't written any code. Someone else wrote the code and tests, so I already know that my class works. I can instead concentrate on writing C, not the mechanics to make OO work. For the detractors in the audience whining about not using C, let me remind you that C can't automatically check the types of the arguments. You can hack around this by overriding C and C, but that sort of defeats the entire purpose of auto-generating accessors. I'll pass. This, of course, is just the tip of the Moose iceberg. Even in the simplest case, you'll save a ton of time and energy, but when you start wanting delegation, stricter type constraints, or introspection, Moose shines even brighter. One more example: package CPAN::Distribution; use Moose; use MooseX::AttributeHelpers; has 'module_versions' => ( metaclass => 'Collection::Hash', is => 'rw', isa => 'HashRef[Str]', default => sub { {} }, provides => { count => 'module_count', keys => 'modules', get => 'module_version', set => 'add_module', }, ); Then I can say: my $dist = CPAN::Distribution->new; $dist->module_versions( { 'Foo::Bar' => '42', ... } ); $dist->add_module( 'Another::Module' => '0.01' ); my @modules = $dist->modules; # Foo::Bar, Another::Module $dist->module_count; # 2 $dist->module_version('Foo::Bar'); # 42 All this from 12 lines of declarations. And I know it works, because someone else wrote (and tested) the actual code. In conclusion, Moose is not an unnecessary dependency that will distract you from your actual problems. It's exactly the opposite; it's a dependency that, for the cost of adding 3 non-core modules to your app, will save you hours of writing code and tests. This is Perl and There's More Than One Way To Do It, so feel free to ignore Moose. But don't be surprised when your clients switch to a company that doesn't waste their money reinventing the OO wheel. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFHSxcx2rw+dVvzZm0RAvPxAJ47bjOldx77+zEME1Xzl3BtbHP1tQCgshK7 KpCStbNpMkkIzxOiURdbOAs= =W+fw -----END PGP SIGNATURE-----