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

Hello Perl Monks, I am facing a problem with the perl modules and dont know how to go about it, i request you for yur assistance. i have a sample perl module called hello.pm and here is the script below.
#!/usr/bin/perl package Hello; use Exporter; @ISA = ('Exporter'); @EXPORT = ('hello'); sub hello { print "Hello, world\n"; } 1;
hello.pl is the script which makes use of Hello.pm
#!/usr/bin/perl use Hello; hello();
I was running these on C:\ and it was working fine, the moment i moved these two into another folder Ex: c:\test \testbvt and tried running them it gave me an error as below.
C:\GSDBVTAS\GSDBVTMAIN>perl hello.pl Can't locate Hello.pm in @INC (@INC contains: C:/Perl/lib C:/Perl/site +/lib .) at hello.pl line 3. BEGIN failed--compilation aborted at hello.pl line 3.
Could you please guide me as to how to go about implementing it. Looking forward for your assistance.

Replies are listed 'Best First'.
Re: Creating new perl modules.
by mulander (Monk) on Oct 26, 2005 at 05:48 UTC
    Add to your script:
    use lib qw(/path/to/module);
    use lib qw(); will tell the script in what directory it should search for your modules.
    Another thing you may want to setup some variables so add this line just after use Exporter:
    use vars qw(@ISA @EXPORTER);
    Hope it helps.

      And, it's worth mentioning a couple of things.

      If your module and the script that's using it are in the same directory, you don't need use lib. However, if you run it like this:

      C:\> perl C:\test\testbvt\hello.pl

      Perl sees C:\ as the current directory, and so can't find the modules. If you cd to C:\test\testbvt first, it should work. If this is the type of situation you have, you are wiser not to use lib, but to specify:

      C:\> perl -IC:\test\testbvt C:\test\testbvt\hello.pl

      The I option specifies a dir to add to @INC for that run only.

      On the other hand, if you have the following files:

      C:\test\hello.pl C:\test\Lib\Hello.pm

      You might want to change your package name to Lib::Hello instead of just Hello. Definitely read the docs for lib and perlmod, which cover how modules are located and how @INC works.

      Two quick last points. First, perl modules are not 'scripts': therefore you don't need (and probably shouldn't use) the #!/usr/bin/perl line in them. That line is a UNIX thing that lets the shell know to invoke the Perl interpreter for the file in question. By the time you import a module, the interpreter is working (not the shell).

      Second, make a habit of including use strict; use warnings; at the top of scripts and modules until you understand when they can/should be turned off. If you get warnings you don't understand, add use diagnostics; as well, which give a more verbose explanation.

      <-radiant.matrix->
      A collection of thoughts and links from the minds of geeks
      The Code that can be seen is not the true Code
      "In any sufficiently large group of people, most are idiots" - Kaa's Law
      Thank You very much!
Re: Creating new perl modules.
by murugu (Curate) on Oct 26, 2005 at 05:22 UTC

    Hi nisha,

    If im not wrong, Usually perl will search for the modules in the locations which are all mentioned in @INC array.

    As your modules are loaded during compile time, you need to mention the location during compilation time.

    BEGIN { unshift @INC,"whateverpath you are moving your pm files"; }

    Then run your code. This may help you.

    Regards,
    Murugesan Kandasamy
    use perl for(;;);