I try to turn Perl closures into Moo accessors, as in the following code. Unfortunately the code works with Moose but throws an exception with Moo.
Please help me to write a Moo code with read and write accessors defined by Perl closures (not the default accessors which just read and store a simple value, but accessors reading and writing which should call my closures).
#!/usr/bin/perl
package X;
use Moo;
my $BusinessClass = "X";
my $Key = 'zz';
no strict 'refs';
*{"${BusinessClass}::access_$Key"} = sub { "Modified $Key" };
has $Key => ( is => 'rw',
required => 0,
accessor => { $Key => \&{"${BusinessClass}::access_$Key"} },
# predicate => { "has_$Key",\&{"${BusinessClass}::access2_$Key"} },
);
my $obj = X->new;
print $obj->zz, "\n";