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

I have created a test.pm perl module in "Module" folder which is a sub direcotry of "plscript" folder.
In "plscript" folder i created a test.pl file and i tried to call the function defined in test.pm.
To include the module in test.pl i am using push(@INC, $SOURCE_ROOT . "/Modules"); use Module::test;
on executing the script i am getting following error.
Can't locate Modules/test.pm in @INC

Replies are listed 'Best First'.
Re: Can't locate Modules/test.pm in @INC
by ysth (Canon) on Feb 22, 2007 at 08:06 UTC
    use happens at compile-time, before your push is executed. Put the code that sets $SOURCE_ROOT and the push in a BEGIN block so it is takes effect before the use.
Re: Can't locate Modules/test.pm in @INC
by klekker (Pilgrim) on Feb 22, 2007 at 08:05 UTC
    Try
    use lib "<FULLPATH HERE TO>/Modules"; use test;


    edit: I prefere "use lib" instead messing around with @INC;

    edit2: Where is my coffee? I deleted my old text: if you have "<FULLPATH HERE TO>/Modules" in your @INC, you have to ommit the "Modules::" Part in "use test". And check your spelling of "module" and "modules"

    k
Re: Can't locate Modules/test.pm in @INC
by Moron (Curate) on Feb 22, 2007 at 10:16 UTC
    Actually,
    use lib $ENV{ SOURCE_ROOT };
    ought to be sufficient. Then all modules in ANYWHERE in the $SOURCE_ROOT tree will be addressable as subdir::subdir:: etc ::

    BUT, rather than have to make a header for ALL your .pl files, it might be worth putting the following in your group login scripts (although in that case for every environment using the software so will need to be put in the next deployment script):

    export PERL5LIB=$SOURCE_ROOT:$PERL5LIB
    which has the same effect, but without needing to modify your .pl files at all!!

    -M

    Free your mind

      Hi I tried both the options.
      When I included the use lib $ENV{ SOURCE_ROOT }; in .pl fine nothig changed, i got the same error again.
      And on trying the second option I was not able to locate the PERL5LIB env variable so created one and when i appended the $SOURCE_ROOT following error i got:
      login-linux-hyd-001{67}> which perl
      /pkg/perl/5.8/bin/perl
      login-linux-hyd-001{68}> setenv
      PERL5LIB /pkg/perl/5.8/bin/perl
      login-linux-hyd-001{69}> setenv PERL5LIB
      $SOURCE_ROOT:$PERL5LIB
      Bad : modifier in $ ($).
      login-linux-hyd-001{70}> setenv PERL5LIB
      $SOURCE_ROOT:$PERL5LIB Bad : modifier in $ ($).
      login-linux-hyd-001{71}> setenv PERL5LIB
      SOURCE_ROOT:$PERL5LIB
      login-linux-hyd-001{72}> echo $PERL5LIB
      SOURCE_ROOT:/pkg/perl/5.8/bin/perl
      login-linux-hyd-001{73}> setenv PERL5LIB
      $SOURCE_ROOT:/pkg/perl/5.8/bin/perl
      Bad : modifier in $ (/).
        The problem indicated by Bad : modifier in $ ($) is a problem with csh variable syntax. The colon has special significance if it follows a variable name. To isolate it, use {} around the variable name, much like in Perl. So
        setenv PERL5LIB ${SOURCE_ROOT}:$PERL5LIB
        should do what you want.

        However, the effort you are making to put (parts of) the original include path into $PERL5LIB is unnecessary. What you specify in $PERL5LIB is searched in addition to the original path. So, just

        setenv PERL5LIB $SOURCE_ROOT
        should be enough.

        Anno

        Unfortunately, I only have access to a sunos system today and I can't reproduce the problem you are now having. However, the impression I get is that SOURCE_ROOT is not yet itself defined when you tried the above. Also, it is worth asking what shell you are using (try the ps command to find out).

        -M

        Free your mind

Re: Can't locate Modules/test.pm in @INC
by syphilis (Archbishop) on Feb 22, 2007 at 08:36 UTC
    If test.pm begins with the line
    package Module::test;
    then you should be able to load your module into any of your scripts (located in the 'plscript' folder) by beginning them with:
    use Module::test;
    (This is reliant upon the fact that '.' is already in @INC.)

    Cheers,
    Rob
Re: Can't locate Modules/test.pm in @INC
by karhu (Novice) on Feb 22, 2007 at 17:24 UTC
    Anyway, you're doing the PUSH at runtime, so the use line gets done first. Put it in a BEGIN {} block if you want, or use 'use lib "."'.
      I experienced a similar problem today.

      For some reason the current working directory wasn't being set to the location where the script is running. It was manifested by the "can't load module ...", "can't call method XYC" ... Templates, config files .... whatever ... everything was coded with relative paths.

      Simplest solution was to set the CWD with :
      cwd '/full/path/here';

      Does anyone know what went wrong - so that CWD isn't automaticly set to location where the script is located?


      Have you tried freelancing? Check out Scriptlance - I work there. For more info about Scriptlance and freelancing in general check out my home node.