in reply to Moose: Accessing subroutines in packages used by Moose class

Updated: Please ignore as this won't be useful for the OP problem due to not handling the package parameter (as noted by Corion).

Moose can create such wrappers for you e.g.
package Bogus; use Data::Dump 'pp'; sub foo { printf "foo got %s\n", pp([@_]) } sub bar { printf "bar got %s\n", pp([@_]) } sub baz { printf "baz got %s\n", pp([@_]) } package Tester; use Moose; has 'bogus' => ( is => 'bare', default => 'Bogus', handles => [qw( foo bar baz )] ); package main; my $tester = Tester->new; $tester->bar('bar'); $tester->foo('ram'); $tester->baz('ewe');
See Moose::Manual::Delegation

Replies are listed 'Best First'.
Re^2: Moose: Accessing subroutines in packages used by Moose class
by Corion (Patriarch) on Apr 18, 2016 at 11:12 UTC

    I think that Moose won't write the code that handles delegating to a function (by shifting $_[0] away). But other than that, letting Moose handle the delegation is certainly a much better approach than handling the delegation yourself (once you've bought into Moose).

      D'oh! You're quite correct, though it can be fixed with additional hackery:
      package Bogus; use Data::Dump 'pp'; sub foo { printf "foo got %s\n", pp([@_]) } sub bar { printf "bar got %s\n", pp([@_]) } sub baz { printf "baz got %s\n", pp([@_]) } package MyBogus; use Moose; use MooseX::NonMoose; extends 'Bogus'; around [qw( foo bar baz )] => sub { my $orig = shift; my $self = shift; $orig->(@_); }; package Tester; use Moose; use MyBogus; has 'bogus' => ( is => 'bare', default => sub { MyBogus->new }, handles => [qw( foo bar baz )], ); package main; my $tester = Tester->new; $tester->bar('bar'); $tester->foo('ram'); $tester->baz('ewe'); __END__ prints: bar got ["bar"] foo got ["ram"] baz got ["ewe"]

        Oooh - I like that use of around for wrapping functions. In my mind, I didn't make the connections between functions and methods and thought of manually writing the wrapper functions, but around already provides all functionality that is needed, at the price of another class/package shoved in between the use and the importing of the code.

        Sorry, I'm pretty new to Moose and I'm not clear as to whether this applies to my situation where my Moose class is using the subroutines pulled in from a non-Moose module.

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