Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: Idea: Moose Mutal Coercion

by duelafn (Vicar)
on Feb 14, 2012 at 15:04 UTC ( #953711=note: print w/replies, xml ) Need Help??


in reply to Re: Idea: Moose Mutal Coercion
in thread Idea: Moose Mutual Coercion

Well, I was going to convert your code into a moose BUILDARGS method, but decided that I think the best approach along this line would be simply lazy-building. Of course, the advantage of the OP's approach is re-usability. The re-usability can be mostly recovered by moving the attributes to a role:

package HumanName; use Moose::Role; use re 'taint'; # Can use isa => first_name_type ... if you prefer for (qw/ first last full /) { has $_."_name => isa => "Str", lazy_build => 1, predicate => "has_ +${_}_name"; } sub _build_full_name { my $self = shift; if ($self->has_last_name and $self->has_first_name) { return join " ", $self->first_name, $self->last_name; } if ($self->does("Gender")) { return "John Doe" if $self->has_gender and "M" eq $self->gende +r; return "Jane Doe" if $self->has_gender and "F" eq $self->gende +r; } # ... whatever complex constructions we like die "Can not build full name"; } sub _build_first_name { my $self = shift; return (split /\s+/, $self->full_name)[0];# or more complex chain. +.. } sub _build_last_name { my $self = shift; return (split /\s+/, $self->full_name)[1];# or more complex chain. +.. }

Though, hopefully you intend to use these methods only to provide (semi-sane) defaults when first/last/full are needed but not known - names are too complex for the above to be reliable generally.

Good Day,
    Dean

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://953711]
help
Chatterbox?
[LanX]: maybe better /msg discipulus ?
[atcroft]: I thought I saw them on, and just wanted to let them know.
[LanX]: hmm ... or even better /msg /msg [discipulus]
LanX better!
[atcroft]: .oO(Plus it was a little slow for a few minutes, at least... ;) )

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2018-02-24 20:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (311 votes). Check out past polls.

    Notices?