[Melbourne-pm] Moose - complex validation

Scott Penrose scottp at dd.com.au
Tue Jan 19 21:25:32 PST 2010


Hey Team

I have a complex moose validation, dependent on multiple inputs.

e.g.

package MyThing;
use Moose;

subtype 'Path'
	=> as Str
	=> where { -d $_ }
	=> message { "Path provided must exist" };

subtype 'Command'
	=> as Str,
	# XXX how to get root ?
	=> where { -f "$root/$_" && -x "$root/$_" },
	=> message { "Command provided does not exist or is not executable " };

had 'root' => (
	is => 'ro',
	isa => 'Path',
	default => '/usr/bin',
);

has 'script' => (
	is => 'ro',
	isa => 'Command',
	required => 1,
);

Basically you would create the object

my $o = MyThing->new(
	script => 'example',
);

but you could override the default root for the scripts with:

my $o = MyThing->new(
	script => 'example',
	root => '/usr/sbin',
);

So... as you can see form the subtype 'Command' I am not sure how you can access the other parameter, and to define the validation order, as root would need to be done first.

So... should it be done in a BUILD ?

sub BUILD {
	my $self = shift;
	if (! -x $self->root . '/' . $self->script) {
		die "Help... script should exist in path and be executable...";
	}
}

I.e. is 'subtype' only for primitives ?

Scott


More information about the Melbourne-pm mailing list