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

Hi

I want to use Moo in scripts where startup time matters.

Some of this scripts might be used in conjunction with modules from authors which insist on using Moose. Now the docs say https://metacpan.org/pod/Moo#MOO-AND-MOOSE

> If Moo detects Moose being loaded, it will automatically register metaclasses for your Moo and Moo::Role packages, so you should be able to use them in Moose code without modification.

Does this mean if someone else is using Moose in the depth of the modules I use, then my script will "auto-migrate" to a Moose backend too?

update

And what if a Moose module is loaded after a Moo module?

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re: "Moo detecting Moose" mechanics?
by choroba (Cardinal) on Apr 18, 2019 at 16:23 UTC
    I found an easy way to experiment with it. Use Moo::sification as an indicator:
    #! /usr/bin/perl use warnings; use strict; use feature qw{ say }; #use Local; { package My; use Moo; has x => (is => 'rw'); } my $o = 'My'->new(x => 12); #say for $o->meta->get_method_list; #say $o->meta->isa('Moose::Meta::Class'); require Moo::sification; Moo::sification->unimport();

    where Local uses Moose.

    Uncommenting either the use Local or any of the ->meta lines makes the unimport fail with

    Can't disable Moo::sification after inflation has been done

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Thanks to you and Corion. :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: "Moo detecting Moose" mechanics?
by Corion (Patriarch) on Apr 18, 2019 at 16:24 UTC

    The documentation also mentions no Moo::sification; to prevent interoperability with Moose. Looking in the source code Moo::sification points to Moo::HandleMoose, which likely handles Moose. I did not look into that module.

Re: "Moo detecting Moose" mechanics?
by tobyink (Canon) on Apr 19, 2019 at 00:09 UTC

    Once Moose is loaded, you can do this with any Moo class:

    my $class = ref($object); my $meta = $class->meta; ...; # do stuff with $meta

    The Moo class will act like any other Moose class. (But it keeps its original inheritance, inheriting from Moo::Object and not Moose::Object.) It will have a metaclass that you can introspect.

    Yes, this works even if Moose gets loaded later. (Moo detects Moose being loaded by performing a tie on $Moose::AUTHORITY to an object with a DESTROY method, so it notices when Moose.pm loads and sets $Moose::AUTHORITY.)