Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Passing an import list to Moose extends

by LanX (Saint)
on May 04, 2018 at 22:45 UTC ( [id://1214082]=note: print w/replies, xml ) Need Help??


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

> 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

Replies are listed 'Best First'.
Re^4: Passing an import list to Moose extends
by davido (Cardinal) on May 05, 2018 at 05:54 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1214082]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-26 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found