in reply to dynamically expand @INC beyond BEGIN

One can modify @INC any time one pleases. Whether it's in a BEGIN block, or outside of it. However, for perl to find a module, the directory the module is to be found in should be added before perl goes looking for it. Considering that use is done at compile time, using a BEGIN (one which preceeds the use) is a simple way of enforcing @INC to be set in time.

Using require and postponing the use of a module to run time, obviously works as well.

Doesn't the snippet you gave work?

Replies are listed 'Best First'.
Re^2: dynamically expand @INC beyond BEGIN
by somekindafreak (Acolyte) on Apr 17, 2012 at 01:52 UTC
    feeling stupid at the moment.

    All morning it was continually saying cannot find MODULE.pm in <@INC_DIRS>, with the front dir being the one loaded that has the module in it. I couldn't figure why it would work when defined in the BEGIN block, but not in my code body and thought it had to be something to do with perl/@INC. Tried it one more time in the body and now it's working just fine. No idea what I had wrong there previously.

    Thanks for your help.

      Note that require "$lib/Module.pm" would also have worked if $lib contains an absolute directory (and probably even if it doesn't?)

      You could also make the change to @INC temporary using local @INC = ( $lib, @INC );.

        thanks, if $lib/$module.pm had all the code I needed then require would work. Instead, it contains about 30 use statements for other modules where the all code is stored, hence the reason why @INC needs $module loaded.