Home: Perl Programming Help: Advanced:
Custom accessors work in Moose but not in Moo



porton
Novice

Sep 8, 2016, 7:08 AM


Views: 6851
Custom accessors work in Moose but not in Moo

The following Perl code works with Moose but does not work (throws an exceptions) with Moo.


Code
#!/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";


My real-life problem is how to generate classes whose fields are accessed through custom accessors. With Moose it can be accomplished by code like the above. But how to do this with Moo?


(This post was edited by porton on Sep 8, 2016, 7:09 AM)


porton
Novice

Sep 8, 2016, 8:26 AM


Views: 6842
Re: [porton] Custom accessors work in Moose but not in Moo

Do you see what I need?

I want to make Moo accessors which use Perl closures (a different closure for each accessor).

How to do this?