Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Idea: Moose Mutal Coercion

by educated_foo (Vicar)
on Feb 14, 2012 at 12:13 UTC ( #953692=note: print w/replies, xml ) Need Help??

in reply to Idea: Moose Mutual Coercion

It seems like you're making your life harder than you have to. You would be much better off picking one standard data representation and supplying multiple initialization functions, e.g.:
package Identity; sub init_first_last { my ($c, $first, $last) = @_; die unless $first =~ /^\S+$/ && $last =~ /^\S+$/; bless { first => $first, last => $last }, $c; } sub init_fullname { my $c = shift; my ($first, $last) = split ' ', shift; init_first_last $c, $first, $last; } sub fullname { join ' ', @{$_[0]}{qw(first last)}; } sub first { shift->{first}; } sub last { shift->{last}; }

Replies are listed 'Best First'.
Re^2: Idea: Moose Mutal Coercion
by duelafn (Vicar) on Feb 14, 2012 at 15:04 UTC

    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,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://953692]
[stevieb]: heh, yeah, sorry. This is integration testing for certain. In fact, it's even Continuous Integration ;)
[stevieb]: Obviously, Travis CI just won't cut it for these distributions...
[stevieb]: I went on my merry way writing a cross-platform, network-aware system that works across Perlbrew and Berrybrew systems and runs unit tests for Perl dists on all installed versions, with the ability to manage *brew commands themselves
[stevieb]: That worked out exceptionally well, as when I started that project, I hadn't delved into hardware development yet.
[stevieb]: found a issue in MetaCPAN::Client though today for my revdep tests. At least I think it's an issue

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2017-06-25 23:29 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (572 votes). Check out past polls.