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

I have a list of modules which, for simplicities sake I shall say live in a directory /modules/xxxx/ where xxxx is pseudo random (certainly not known at time of writing).

There is a common perl package which is used by most of these modules, they will also require their own packages etc.

for simplicities sake, it would be nice if I could have a "use common qw( :standard );" line common to each module, with appropriate "use lib '/systemlibs'"; This bit I have working quite happily.

The trouble is, I'd also like to be able to include /modules/xxxx/libs by default, with this detail being implicitly set by the use common bit.

The only trouble is, the "common" module is unaware of the modules name (the xxxx bit) until runtime, and use lib "..." is deduced at compile time.

Does anyone have any ideas?

Replies are listed 'Best First'.
•Re: Runtime use of "use lib <xxx>"
by merlyn (Sage) on May 19, 2004 at 15:37 UTC
    use lib xxx at runtime is effectively unshift @INC, xxx. However, you'll have to have that runtime take effect before you compile further things with use, so I'm not sure exactly how early you need it done.

    Also note that this works:

    my $extra; BEGIN { $extra = "some computation here" } use lib $extra;

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Runtime use of "use lib <xxx>"
by Somni (Friar) on May 19, 2004 at 23:54 UTC

    The simplest method of modifying @INC at runtime has been mentioned; it is, after all, just an array.

    If you want all of the semantics of use lib at runtime you can also use:

    use lib; ...; lib->import($path);

    I occasionally use this idiom with use base.

      Cheers for that, I had kinda guessed at pushing things onto the @INC array, but sadly can't test it too easily at the moment (need to get someone to write a "module" really).

      The main reason I asked was that the local PerlMonkey (tm) declared that @INC was only evaluated at compile time so would therefore negate what I was trying to do.

      I shall go and give him a jolly good slap :)