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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|