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

How to add to @inc?

My current @inc is c:/perl/lib and c:/perl/site/lib. How do I add so perl will also search the folder where I am runing the script?. I read in about.com that "." is use so that perl will search from the current folder.

But how to add "." in @inc?

janitored by ybiC: Retitle from less-than-descriptive "@inc" because one-word nodetitles hinder site search, also minor format tweaks for legibility.

Replies are listed 'Best First'.
Re: Add current working directory to @INC?
by gaal (Parson) on Jul 18, 2004 at 06:32 UTC
    The variable name is spelled @INC, in uppercase.

    Modify it as you would any Perl array: unshift @INC, "some/path";

    You need to do this before you actually attempt to load any modules in the new path, so you put it in a BEGIN block:
    BEGIN { unshift @INC, "some/path"; }

    Or, you can use the lib pragma to do this for you:
    use lib "some/path";

    Finally, in this case as you mentioned, "." is already in the module search path, so you can do nothing. :-)

      lib does more than just unshifting. Amongst others, it also removes trailing identical elements.

      Note that thus giving lib something that already exists in @INC will move it to the beginning.

      Under taint mode, "." is not in @INC.

      ihb

Re: Add current working directory to @INC?
by Anonymous Monk on Jul 18, 2004 at 09:51 UTC
    "." is not in the module search path for my computer, I am using WindowXP and Activeperl. So how to make "." as one of my @INC?. TQ
      Are you "deaf" or something?
      use lib '.';

      To be on the safe side, and if you plan on chdirring in your script, it's safer to do

      use Cwd; use lib cwd;
      cwd(), short for the "current working directory", is a function in Cwd that returns the absolute path for the current directory, thus ".".

      More flexible still is something like

      use File::Spec::Functions 'rel2abs'; use lib rel2abs('.');
      because this way it's easy to add an absolute path out of a specified relative path, say a subdirectory of the current directory:
      use File::Spec::Functions 'rel2abs'; use lib rel2abs('lib');

      In general, I prefer to use the functions from File::Spec::Functions, but if you don't use it for anything else, I'd avoid the namespace pollution from importing the sub name, and use the more convoluted syntax using the methods:

      use File::Spec; use lib File::Spec->rel2abs('lib');
      I have winXP, ActiveState and a dot.
      for (@INC){ print "$_\n" }
      outputs:
      C:/Perl/lib C:/Perl/site/lib .
      In a cgi script with taint on the dot gets stripped. Is that what you are doing?
        same setup here, and it looks in the script dir just fine.