Moose can take care of this kind of thing very easily. Here is a simple translation of what you did.
package MyClass;
use Moose;
use Moose::Util::TypeConstraints;
has 'fname' => (
is => 'rw',
isa => subtype('Str', where { /^[\w-]+$/ }),
);
The 'isa' becomes a anon-subtype of "Str" (the String type in Moose) but with the additional validtion in the where clause. A better way to do this though would be make it an official subtype, so that you can then re-use it in other attribute accessors.
package MyClass;
use Moose;
use Moose::Util::TypeConstraints;
subtype 'StringWithDashes'
=> as 'Str'
=> where { /^[\w-]+$/ };
has 'fname' => (
is => 'rw',
isa => 'StringWithDashes'
);
has 'lname' => (
is => 'rw',
isa => 'StringWithDashes'
);
Moose can also handle delegating the validation as well, this would match the example of one of the other posters.
use Email::Valid;
has 'email' => (
is => 'rw',
isa => subtype('Str', where { Email::Valid->address($_) })
);
Of course this only barely scratches the surface of accessor validation in Moose. You can also set attributes to be required in the constructor, you can set defaults for those attributes as well. Then you can get into coercion, which allows you to easily convert one value into another. The Moose::Cookbook has examples for all these things. And if you want to extend those same types and use them in method validation as well, there is the more lightweight MooseX::Params::Validate and the more featureful MooseX::Method to choose from.
|