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

Title pretty much says it all, I've been reading and googling but didn't find much - is there a quick and easy way to manipulate @INC (specifically, add to it) at the start of a script?

Sorry if it's basic stuff, once again I'm really new at this. :) Thanks very much!

Replies are listed 'Best First'.
Re: How to manipulate @INC in a script?
by ikegami (Patriarch) on Sep 21, 2004 at 14:41 UTC

    use lib '/home/me/lib';
    adds the directory to the front of @INC at compile time.

      In addition to use lib, you can also use -I<path> on the command line like:
      $ /usr/bin/perl -I"/home/me/lib" myscript.pl
      or in your shebang line like:
      #!/usr/bin/perl -I"/home/me/lib"
      --
      $me = rand($hacker{perl});

      All code, unless otherwise noted, is untested

        Don't know if this is true just of Win32 (running ActivePerl 5.6.1 (633)) but if you use quotes in the shebang line it doesn't work.

        i.e.

        #!/usr/bin/perl -I"/somedir/"

        ..won't work but

        #!/usr/bin/perl -I/somedir/

        .. will.

        The /usr/bin/perl wouldn't be strictly correct on Win32, but under ActivePerl it doesn't seem to matter (could set it to the proper DOS path if required).

      Well, I feel silly now. :) Thank you very much!
Re: How to manipulate @INC in a script?
by Roy Johnson (Monsignor) on Sep 21, 2004 at 14:57 UTC
    perldoc -q "include path"

    Caution: Contents may have been coded under pressure.
      Mmm. I never had problems pushing to @INC at the start of my scripts, though you're probably right that messing with it later on isn't going to do anything.
Re: How to manipulate @INC in a script?
by punkish (Priest) on Sep 21, 2004 at 14:41 UTC
    @INC is just an array, so just push(@INC, 'yet/another/path'); and be done.
      punkish,
      @INC is not just any array. It exists before runtime. Try modifying it at run time and have it work for a use statement that works at compile time - it no workie.
      BEGIN { unshift @INC , 'yet/another/path' }
      That will likely work in most cases, but you are better off using the lib pragma as perldoc perlvar suggests or the PERL5ENV variable.

      Cheers - L~R

        Thanks L~R (and, apologies to the OP for a misleading reply).

        I am just not too clear on compile-time vs. runtime (hence, an additional poor understanding of use vs. require). I will go back to the docs and readup. Hopefully, I will get it this time.