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

This doesn't seem to work for me:

use v5.14.4; use warnings; use autodie qw< :all >; use Moops; role Foo { #use Moose::Role; use Data::Printer; p __PACKAGE__->meta; } 1;

First of all, should it? If not, why not? Note that if you uncomment the commented line, then it works fine. But that seems redundant to me.

To clarify, when I say "doesn't work," what I mean is:

Can't locate object method "meta" via package "Foo" at test.pm line 12 +. BEGIN failed--compilation aborted at test.pm line 13.

What I really want to do is this:

role Foo { use Moose::Util; Moose::Util::meta_attribute_alias 'Foo'; }

but not being able to get the meta means that fails as well:

Can't call method "isa" on an undefined value at /home/buddy/perl5/perlbrew/perls/perl-5.14.4/lib/site_perl/5.14.4/x86_64-linux/Moose/Util.pm line 335.

Enlighten me, O fellow monks. What is the magical Moops incantation I'm missing here?

Replies are listed 'Best First'.
Re: Moops: meta not available in role
by kevbot (Vicar) on Jul 28, 2015 at 06:35 UTC
    Hi Oberon,

    I have not really used Moops; however, it appears that Moops uses Moo by default. The Moops documentation states Moops uses Moo to build classes and roles by default, but allows you to use Moose if you desire. (And Mouse experimentally.).

    According to the INCOMPATIBILITIES_WITH_MOOSE section of the Moo documentation, Moo does not have a meta object.

    So in order to get the meta object you need to tell Moops to use Moose. See the Roles section of the Moops documentation.

    Adding using Moose to your code fixes the issue.

    use v5.14.4; use warnings; use autodie qw< :all >; use Moops; role Foo using Moose { use Data::Printer; p __PACKAGE__->meta; } 1;
      According to the INCOMPATIBILITIES_WITH_MOOSE section of the Moo documentation, Moo does not have a meta object.

      AHA! I had totally forgotten that about Moo. That is exactly the issue. (And I totally should have known that by the fact that I was calling a Moose::Util method ...)

      Adding using Moose to your code fixes the issue.

      Well, yes, but what I really wanted was a way to do what I was trying to do in Moo. But, now that I look at it more closely, I don't believe Moo supports traits at all ... not even in any sort of MooX module. Possibly Moo is unable to do attribute traits due the lack of a MOP. So I guess do need to take your suggestion and just tell Moops to use Moose everywhere. (To wit, I think I'll use MoopsX::UsingMoose.)

      So here's some code which works:

      use MoopsX::UsingMoose; use autodie qw< :all >; role Foo { use Data::Printer; p __PACKAGE__->meta; }

      Note that, after more careful reading of the Moops docs, I figured out that I don't need the use v5.14 line, nor the use warnings line, nor even the 1; at the end of the module: Moops itself will do all that for me. Cool.

      Thx for the assist, kevbot.

        I figured out that I don't need the use v5.14 line, nor the use warnings line, nor even the 1; at the end of the module: Moops itself will do all that for me.

        FYI if you are writing a module for CPAN distribution, you will be penalized in kwalitee if you omit use warnings; or use strict; from your code, even if you are using Moo(se|ps). Not sure about the other "features."

        The way forward always starts with a minimal test.
Re: Moops: meta not available in role
by Anonymous Monk on Jul 28, 2015 at 03:15 UTC
      ... to find out use perl -MO=Deparse,-p yourprogram.pl

      Thanks for reminding me of that one; I can never remember that incantation. I've now made an alias for it so now I never have to. :-)