This is a very simple question, but I was still studying and could not find the answer.
You need to check the validity of the provided (required) argument for the Moose object constructor, for example. as in the following example:
use 5.016;
use warnings;
package My {
use Moose;
has 'mydir' => (
is => 'ro',
isa => 'Str',
required => 1,
);
}
use File::Path qw(remove_tree);
package main {
my @dirs = qw(./d1 ./d2);
remove_tree($_) for ( @dirs );
mkdir $dirs[0] or die;
foreach my $dir( @dirs ) {
my $m = My->new( mydir=>$dir );
say "$dir ", defined($m) ? "" : "NOT", " ok";
}
}
Question: what should I add to the package Myto ensure that the object My is created only if the provided path mydirexists? So somewhere you need to add a test if -d ....
How to define a mydirvalidation attribute ?
The result of the main program is required:
./d1 ok
./d2 NOT ok
source
share