Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Referencing subroutine in not-yet-loaded module

by vsespb (Chaplain)
on Nov 05, 2015 at 10:03 UTC ( [id://1146978]=perlquestion: print w/replies, xml ) Need Help??

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

Is this good practice to write code like this:
my $subref = \&NotYetLoadedModule::mysub();
or it's a bug, and only possible way to get reference to such subroutine is:
my $subref = sub { &NotYetLoadedModule::mysub };
I am asking because after 1st example something is broken, and Class::Load::load_class refuses to load NotYetLoadedModule (this differs from require behaviour and thus I filed a bug report)

Replies are listed 'Best First'.
Re: Referencing subroutine in not-yet-loaded module (ancient)
by tye (Sage) on Nov 05, 2015 at 15:55 UTC

    Being able to take a reference to a sub that has not yet been defined has been a feature in Perl "forever", and it seemed to me to not be an accidental result of some implementation detail. So I see no reason to go with a more complex approach.

    - tye        

Re: Referencing subroutine in not-yet-loaded module
by GotToBTru (Prior) on Nov 05, 2015 at 13:42 UTC

    The alternatives are .. is this good practice or is it a bug? That's confusing to me. You said it works .. but you are filing a bug report? That's even more confusing.

    Referencing a not yet loaded module in order to trigger loading on demand might be a great practice for saving on load time and memory requirements, but I would make sure this is well documented in your code.

    Dum Spiro Spero
      You said it works .. but you are filing a bug report?
      What works is references to subroutine in not-loaded packages. Bug report against Class::Load, which refuses to load packages after such referencing (thanks that I still have "require" working).
      is this good practice or is it a bug?
      Exactly. I think those are more probable alternatives.
      Referencing a not yet loaded module in order to trigger loading on demand might be a great practice for saving on load time and memory requirements
      I am not asking about such references in general, only about \&NotYetLoadedModule::mysub(); vs sub { &NotYetLoadedModule::mysub };
Re: Referencing subroutine in not-yet-loaded module
by Anonymous Monk on Nov 05, 2015 at 10:32 UTC

    Is this good practice to write or its a bug...

    Does it work?

    I am asking because after 1st example something is broken, and Class::Load::load_class refuses to load NotYetLoadedModule (this differs from require behaviour and thus I filed a bug report)

    Well, Class::Load isn't require

    The documentation is pretty clear, it uses "heuristics" to determine if a class is loaded, require doesn't do that, so not a bug in Class::Load behaviour ...

    its a documentation bug, it should document the actual heuristics, not make an allusion to the origin :)

    When I need to load I use the core Module::Load

      Does it work?
      yes. but that does not mean that it's a good practice.
Re: Referencing subroutine in not-yet-loaded module
by Anonymous Monk on Nov 06, 2015 at 21:36 UTC
    The extra parentheses are not necessary (and indeed if you look at this expression in perl -MO=Deparse,-p you'll see they aren't being parsed at all how you expect): my $subref = \&NotYetLoadedModule::mysub; is fine.

    Class::Load looks in the symbol table in order to check if a module is "loaded" (this is to allow for inlined packages, that didn't come from a file).

    The issue was filed as https://rt.cpan.org/Ticket/Display.html?id=108388 and is not resolvable as this is simply how Class::Load works. Generally you're better off using Module::Runtime::require_module, which looks in %INC, a much more reliable mechanism to tell if a module has loaded.

    -ETHER (Karen Etheridge)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1146978]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found