nysus has asked for the wisdom of the Perl Monks concerning the following question:

I'm pretty sure I've made a bad design decision but not sure what else I can do with Moo. I want a attribute to handle methods if they don't exist:

package Mac::ApperlScript::App::Finder::Window ; use Moo; use Mac::AppleScript::Glue::Object; use namespace::autoclean; our $AUTOLOAD; has 'obj' => (is => 'ro', required => 1, isa => sub { die 'Improper object passed' unless ref $_[0] eq 'Mac::AppleScript::Glue::Object' }, writer => '_set_obj', ); sub AUTOLOAD { my $s = shift; my @al = split /::/, $AUTOLOAD; my $method = $al[-1]; return $s->obj->$method; }

So I delegate all methods that don't exist to the obj attribute. The problem is that methods like DESTROY seem to get diverted to the AUTOLOAD subroutine. Is there a better way to achieve what I want to do?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Using AUTOLOAD with Moo
by kroach (Pilgrim) on Jan 15, 2019 at 02:13 UTC
    The standard practice is to define an empty DESTROY when you don't have a destructor and use AUTOLOAD.
Re: Using AUTOLOAD with Moo
by kcott (Archbishop) on Jan 15, 2019 at 06:38 UTC
Re: Using AUTOLOAD with Moo
by Your Mother (Archbishop) on Jan 15, 2019 at 14:54 UTC

    I think handles is the clearest and most flexible way to let method calls go through. I use it often when wrapping up objects into an ad hoc API.

    use 5.14.0; use strictures; package SomeMangagedObject { use Moo; sub exterminate { 1 } sub Exterminate { 10 } sub EXTERMINATE { 100 } 1; }; package MyWrApper { use Moo; use Scalar::Util "blessed"; use Carp; has "robot" => is => "ro", required => 1, handles => [qw/ exterminate Exterminate EXTERMINATE /], default => sub { SomeMangagedObject->new }, isa => sub { "SomeMangagedObject" eq blessed $_[0] or confess "Nope" }; 1; }; my $wrap = MyWrApper->new; say $wrap->robot; say $wrap->exterminate;

      That works if you know in advance what methods you need to be handled.

        I'm a whitelist kinda monk. :P And excepting prototyping or personal/non-production stuff I see $AUTOLOAD as false economy.

Re: Using AUTOLOAD with Moo
by Anonymous Monk on Jan 15, 2019 at 09:31 UTC
    I'm pretty sure I've made a bad design decision but not sure what else I can do with Moo. I want a attribute to handle methods if they don't exist

    probably

    better to generate methods than use autoload