Showing posts with label Moose. Show all posts
Showing posts with label Moose. Show all posts

Monday, July 20, 2009

adventures in ignorance: Multiple roles with MooseX::Declare

I am really liking Moose, in particular MooseX::Declare; however, the docs and error messages can be very confusing. For instance, I was trying to create a class that used two roles and kept getting the following error:
expected option name at [path to MooseX/Declare/Syntax/NamespaceHandling.pm] line 45
What this meant was that I needed to say:
class FooBar with (Fooable, Barable) {}
instead of:
class FooBar with Fooable, Barable {}
Now, I should never have gotten that error if I had followed the docs, so lets see what MooseX::Declare has to say about with:
with
    class Foo with Role { ... }

Applies a role to the class being declared.

No parentheses there (and no example of multiple roles). How about the Moose docs then?
with (@roles)

This will apply a given set of @roles to the local class.
Well, this has the parentheses, but let's look at how with is used in the examples:
package MovieCar;

use Moose;

extends 'Car';

with 'Breakable', 'ExplodesOnBreakage';
Hey! Where are the parentheses? In Moose they are optional, and in MooseX::Declare they are optional if you have only one role, but required if you have more than one.