Search code examples
perlreferencemoosewriter

Perl Moose exception when invoking a reference to an attribute's writer method


I am trying to pass a reference to an attribute's writer method to another class method. The setter method is apparently invoked since the print statement is executed. However, the following error results:

Attribute (dir) is required, so cannot be set to undef at Test.pm line 51
Test::_validateDir('Test=HASH(0x2270be0)', 'CODE(0x22a90f8)') called at test.pm line 37
Test::BUILD('test_moose=HASH(0x2270be0)', 'HASH(0x1879728)') called at generated method (unknown origin) line 50
Test:new('Test', dir=>'my_bin') called at ./test_moose.pl line 26

Is there a reason why a reference to the generated writer method cannot be taken or is there a different or better way to do this in Moose?

Here's the code:

package Test;
use Moose;
use File::Spec;

has 'dir' => (is => 'ro', isa => 'Str', required => 1, writer '_set_dir',);

sub BUILD {
    my ($self, $hr) = @_;
    $self->_validateDir(\&_set_dir);
}

sub _validate_dir {
    my ($self, $setter) = @_;
    my $dir = File::Spec->rel2abs($self->dir, '/home/me');
    if (! -e $dir) {
       print "creating directory\n";
       #create directory
    }
    &$setter($dir);
}

Solution

  • Is it right to call the setter as a regular sub? Shouldn't you pass $self as a first argument?