http://qs1969.pair.com?node_id=915804

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

Hi Monks!

I have the following problem that I can't get to work right now: In my parent Moose class I have the following code:

package Parent; use Moose; use Test::Most '!blessed'; sub BUILD { my ($self, $args) = @_; if ($args->{'tests'}) { plan tests => $args->{'tests'}; } }
The child has:
package Child; use Moose; extends qw(Parent); sub run { my ($self) = @_; ok(1, "a simple test"); } __PACKAGE__->new(tests => 1)->run;

However, this doesn't work because the subs of Test::Most are not in the namespace of the child:

Undefined subroutine &Child::ok called at oo_test.pl line 8.

So my question is: Is there a recommended way to export subs of non-Moose classes in Moose?

Thanks!

Replies are listed 'Best First'.
Re: Moose: Exporting non-moose subs to child
by Khen1950fx (Canon) on Jul 21, 2011 at 08:40 UTC
    It's a bug that has been resolved. Here's my workaround:
    package Parent; use Moose; use Test::Most tests => 1, '-Test::Deep'; sub BUILD { my ( $self, $args ) = @_; if ( $args->{'tests'} ) { plan tests => $args->{'tests'}; } } package Child; use Moose; use Test::Most '-Test::Deep'; extends qw(Parent); sub run { my ($self) = @_; ok( 1, 'simple test' ); } __PACKAGE__->new->run;
Re: Moose: Exporting non-moose subs to child
by moritz (Cardinal) on Jul 21, 2011 at 08:04 UTC
    Just
    use Test::Most;

    in Child.

    Just because Moose provides another object model doesn't mean you have to throw away all your other Perl knowledge when you use it :-).

    (The the usual approach is to put the tests into a separate file, not into the class directly).

      I was actually trying to put the use statement into the parent class to get rid of this ugly line in the child:
      use Test::Most '!blessed';

      The !blessed part is required because Moose and Test::Most don't play together well. So now I want to put this in the parent as there will be hundreds of children of this parent and I don't want to repeat this line over and over again.

      If, however, this doesn't work, then I'll just have to live with it.