in reply to Re: Passing an import list to Moose extends
in thread Passing an import list to Moose extends

Was thinking the same. Either use 'use' or call the import function directly.

Looking at this, why trying to call the import functions at all actually? You should always be able to inherit, Since use Foo '-myFeature' (your class is 'used') will eventually have the same effect.

Wonder what will happens with the selected option in case the user of your class will specify a different option. What if you have already 'used' the class with a certain option. Will the import list specified by the user of your class still be used or silently ignored? The 'use' clause is executed only once right? Or will the import function still be executed?

Calling the import function doesn't seem to have anything to do with extends. Just like use base/parent MyBase is not calling an import function either. Is this actually specific to Moose?

Replies are listed 'Best First'.
Re^3: Passing an import list to Moose extends
by LanX (Saint) on May 04, 2018 at 22:45 UTC
    > The use clause is executed only once right? Or will the import function still be executed?

    use is just require + import at compile time.

     BEGIN { require Module; Module->import( LIST ); }

    • The require will happen only once (it sets and checks %INC, there is no point in loading the code multiple times from disk)
    • the ->import() method will (must) be called each (compile) time.
    But I doubt the OP wants to use import for normal exports of functions.

    One is free to use import for any kind of compile time effects (like executing a source filter or activating pragmas or even calling extend() )

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Wikisyntax for the Monastery

      This is correct, and in this case the base class uses -somefeature to activate a pragma, scoped using %^H. People have been wondering how multiple users of the same base class within the same application would be able to keep track of whether -somefeature is in effect. They probably haven't read (or don't recall reading) perlpragma. ...or possibly wish they never had read it. This is obviously not trivial magic.

      So this is a case where a base class is designed to be inherited from, and to optionally provide some additional behavior, tracked as a pragma. The extends mechanism on its own doesn't accommodate this particular need. It's looking like the simplest approach is a use followed by an extends. That's fine, but consider this:

      [non-Moose base class implementing -somefeature pragma] <- [Moose-style base class adapter passing through -somefeature pr +agma] <- [broad range of subclasses that inherit from Moose-style ba +se class, and that may or may not want to invoke -somefeature]

      We've been able to manage the pass-through just fine, but extends alone isn't sufficient for making it work without use.

      package NonMooseBase; sub import { # I'll spare you the treachery. } ... 1; package MooseBase; use Moose; use NonMooseBase; extends 'NonMooseBase'; ... 1; package MySubclass; extends 'MooseBase'; use MooseBase -somepragma; ... 1;

      This probably will work. As long as inheritance is in place, the presence of the import list in use MooseBase -somepragma will cause ->import() to be called, and it gets inherited from NonMooseBase. We're already handling inheritance depth correctly and twiddling %^H appropriately. The one fly in the ointment was providing a Moosey intermediate class to allow new work to benefit from Moose without losing the useful tools built into the old-school base class.


      Dave

        I have trouble to understand the concept of a lexically scoped base class, and that's what %^H is normally used for.

        But I think instead of hacking Moose, you should try to hide all magic inside an MagicBase::import() which calls extends in the callers package for you and just do use MagicBase qw/-feature/ .

        Like that you avoid boilerplating hacks and you can put all magical documentation inside your module.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Wikisyntax for the Monastery