Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Use cases for hooks in @INC?

by LanX (Saint)
on Apr 20, 2016 at 21:25 UTC ( [id://1161029]=perlquestion: print w/replies, xml ) Need Help??

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

Perl has a powerful feature to include hooks in @INC. (see require for details)

These are basically callbacks which can install dynamically generated modules instead of reading the file system.

I'd be curious about typical use cases for this feature or know projects which apply this technique.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re: Use cases for hooks in @INC?
by belden (Friar) on Apr 21, 2016 at 05:40 UTC
    One use is to observe the dependency graph of your codebase. You can do some things with an @INC hook that you can't do by overloading CORE::GLOBAL::require.

    One of the things you can do with an @INC hook is signal the interpreter to keep looking for a file on disk, or fake out that you've got a module that you don't really have.

    I've found that most of the time I want my @INC hooks to live at $INC[0] or at $INC-1, depending on whether I'm writing an observer or a fallback hook. Simply unshifting (or pushing) your hook where you want it isn't sufficient, since other modules you load can and will tamper with the ordering of things in @INC. "use lib", for example, will push to @INC, and if your hook depends on being the last most entry in @INC, then now you're broken. Tieing @INC is the only way I've found to ensure my hooks stay where I think they belong.

    I wrote Array::Sticky::INC to ensure @INC hooks stay where they belong; you might have a look at that module, and some of the documentation I wrote up as part of it, for a bit more detail and use cases.

Re: Use cases for hooks in @INC?
by Discipulus (Canon) on Apr 21, 2016 at 07:38 UTC
    for completness i'd add How to use @INC coderef hooks

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Use cases for hooks in @INC?
by Anonymous Monk on Apr 20, 2016 at 22:11 UTC
      Thanks, cpan-grep is a clever approach.

      Using the word hook instead of \\& gives additional hits.

      > So the basic use case is the same as for Filter::Simple

      Altering existing modules is one use case.

      Another is loading existing modules from other sources like usb-sticks or via http.

      I wonder if there is a use case where a module's source is generated on the fly.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Re: Use cases for hooks in @INC?
by polettix (Vicar) on Apr 24, 2016 at 21:40 UTC
    Probably in the same ballpark as PAR, you can find an example at work here: I use it so that I can embed the full text of a module in a script, and load it dynamically. I guess FatPacker does something similar too.

    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Io ho capito... ma tu che hai detto?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-23 17:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found