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

I want to extend the contents of the @INC array using the
-I command line argument
The test Perl is below
use strict "vars"; my $inc_item; foreach $inc_item (@INC) { print "$inc_item\n"; }
I used this with the follwing command line
C:\perl-programs>test-inc-iarg.pl -I c:\perl-programs\modules

The print lines gave the following
C:/perl5/lib
C:/perl5/site/lib
.

As can be seen the addtional directory was not shown in the @INC array.
Can anyone explain why this and what I should do so that I can use
the -I argument?
The folder options have been set so that files with extension .pl are processed
as shown next
C:\perl5\BIN\PERL.EXE %1 %*

Replies are listed 'Best First'.
Re: Problem using -I argument with Perl
by imp (Priest) on Jan 10, 2007 at 16:24 UTC
    This provides an additional library path in @INC:
    perl -Ifoo script.pl
    This provides arguments to the perl program in @ARGV:
    perl script.pl -Ifoo
Re: Problem using -I argument with Perl
by rinceWind (Monsignor) on Jan 10, 2007 at 16:56 UTC

    As imp has said, the -I option is a feature of the parsing of the command line, when the command is "perl". Unfortunately, you don't get the ability to jack in such command line options to perl, when you are using a file type association for .pl type files.

    You can get some command line options by passing them in on the shebang (#!) line:

    #!perl -wT

    Not sure if this would work with -I on Windows: try it. If this doesn't work, there are two other approaches that will definitely work: set PERL5LIB=C:\perl-programs\modules (semicolon separated list of paths on Windows) or add a use lib into the script.

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

      In Windows, Perl checks the shebang line for options.
      >type script.pl #!perl -Imoo $,=", "; $\="\n"; print @INC; >perl script.pl r:/Utils/perl/lib, r:/Utils/perl/site/lib, ., moo

      -I... adds to the end @INC.
      -Mlib=... adds to the front of @INC. (better)

      Many thanks for the two replies.
      I favoured the use lib option and tried to use this so that the code became.
      use strict "vars"; my $inc_item; use lib "C:\\perl-programs\\modules"; foreach $inc_item (@INC) { print "$inc_item\n"; }
      This worked as required and gave the following from the print
      C:\perl-programs\modules
      C:/perl5/lib
      C:/perl5/site/lib
      .
      I then tried to use a moudule in the new directory with a new line at the end of the code above of
      use modules::test-module
      However, this did not worked as I then got the following
      Can't locate modules/test.pm in @INC (@INC contains: C:\perl-programs\modules C:
      /perl5/lib C:/perl5/site/lib .) at C:\perl-programs\test-inc-iarg.pz line 13.
      BEGIN failed--compilation aborted at C:\perl-programs\test-inc-iarg.pz line 13.

      It seems that the lines are not processed in order so that it 'got' to the
      use modules::test-module
      before the use lib line even though this nearer the beginning.
      How can this be made to work?

        Name your module with an underscore instead of a hyphen

        --

        Oh Lord, won’t you burn me a Knoppix CD ?
        My friends all rate Windows, I must disagree.
        Your powers of persuasion will set them all free,
        So oh Lord, won’t you burn me a Knoppix CD ?
        (Missquoting Janis Joplin)

        With the line
        use modules::test-module;
        you are trying to use

        C:\perl-programs\modules\modules\test-module.pm

        I suspect you mean to use

        C:\perl-programs\modules\test-module.pm

        For that, the correct syntax is:
        use test-module;
        because you directory C:\perl-programs\modules is already in the INC path.