in reply to Re^4: Moose: Where to define a method required by an attribute trait?
in thread Moose: Where to define a method required by an attribute trait?

It works, but passing an attribute name to the trait and then passing an instance around seems not very elegant to me. Is there any other way to access the class instance from within an attribute trait method?

I would suggest actually that you not pass an attribute name, but instead pass a method name. Then if you put the method name into a common key like 'parameter_callback' instead of the more specific 'attr_valid_domain' you could likely move all the code up into the My::Config role. Here is an (untested) example of what I am talking about:

with My::Config; ## some runtime information, eventually for the trait's method has validDomain => ( isa => 'Str', is => 'rw', ); has alarmMailsTo => ( traits => ['ConfigEmail'], conf_key => 'ALARM_MAILS_TO' parameter_callback => 'validDomain' ## give a method name isa => 'ArrayRef[Str]', is => 'rw', ); .. ## Class role role My::Config { method load() { .. ## $attr is a Moose::Meta::Attribute my $parameter_callback = $attr->parameter_callback; $attr->parse($configString, $self->$parameter_callback()); .. } } ## Base Attribute Trait role My::Config::AttrBase { has parameter_callback => ( isa => 'Str', is => 'rw', required => 1, ); } ## Attribute Trait role My::Config::ConfigEmail with My::Config::AttrBase { method parse($value!, @parameters) { # now this is a more generic API } }
This has a couple benefits:
  1. Your parse API is now more generic and requires less knowledge of the outside world.
  2. You can easily pass multiple parameters using the parameter_callback since it is just a method name. None of the other code will care if that code is an accessor to an attribute or a regular old method.
  3. You now have room to grow with your parameters. If your $boss comes to you tomorrow and says "The configEmail role needs more parameters". you don't have to worry because you can just write a method to wrap "validDomain" and whatever the other parameter is. Then you just change the appropriate code in 'parse' and your done.
Of course, this is Perl so there is always more than one way to do it and there is nothing wrong with your approach really.

-stvn

Replies are listed 'Best First'.
Re^6: Moose: Where to define a method required by an attribute trait?
by maxhq (Novice) on May 04, 2010 at 09:38 UTC

    Thank you very much for you suggestions and for diving into my problem! I like the idea of a more generic API very much and will change it like that.

    Moose made me a very addicted fan of Perl again after I abandoned it for some time...
    This is very elegant programming - I raise my hat to the Moose authors!

    -maxhq