http://www.perlmonks.org?node_id=981891

programmer.perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone,

I wrote a module House.pm and I save it in a directory /home/hamidjon/Perl/Learning/code_examples/myModules

The script of a House.pm is:

package House; # Class sub new { # Class method called a constructor my $class = shift; my $ref={ "Owner"=>undef, # Attributes of the object "Price"=>undef, # Values will be assigned later }; bless($ref, $class); # $ref now reference an object in this class return $ref; # A reference to the object is returned } 1;

To use this module I'm adding this line to my program: use lib ("/home/hamidjon/Perl/Learning/code_examples/myModules");

Am I doing right by adding this line? Are there any other (most correct) ways to use? What does mean the code line: unshift(@INC, "directory"), first I used unshift(@INC, ""); method but it didn't work, then I used 'use lib ("directory");' and it worked, is it true? Thank you...

Code of program that uses the House.pm module:

#!/usr/bin/perl -w use lib ("/home/hamidjon/Perl/Learning/code_examples/myModules"); use House; my $houseref = House->new(); print "\$houseref in main belongs to class ", ref($houseref), "\n";

Replies are listed 'Best First'.
Re: using the Modules
by zentara (Archbishop) on Jul 15, 2012 at 13:04 UTC
Re: using the Modules
by CountZero (Bishop) on Jul 15, 2012 at 17:47 UTC
    The documentation of the lib pragma says:
    This is a small simple module which simplifies the manipulation of @INC at compile time. It is typically used to add extra directories to perl's search path so that later use or require statements will find modules which are not located on perl's default search path.
    It is the recommended way of dealing with modules in a non-standard path.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: using the Modules
by Anonymous Monk on Jul 15, 2012 at 13:08 UTC
    Remove the use lib line you could try the following options:
    1) export PERL5LIB="/home/hamidjon/Perl/Learning/code_examples/myModules"
    2) perl -I/home/hamidjon/Perl/Learning/code_examples/myModules yourScript.pl
    3) perl -V will show you a list of system direcotries where perl will look for modules. Once you module is ready you might want to put them there
      Just to clarify for programmer.perl, in case it isn't clear to him/her: these need to be done at the command line, not inside the script.

        Yes, I didn't know that this line must be written in command line. Instead, I was writting any similar line to my program. Also, now I made it clear for myself the factors like "compile time" and a "run time"...

        Thanks, now I configured my module directories, afterall, I'll write at top line of my programs (that uses modules) just this line: 'use Module_name;':

        (at command line) export PERL5LIB="/home/hamidjon/Perl/Learning/code_examples/myModules"
        ...and I check using 'perl -V':
        Built under linux Compiled at Mar 23 2012 17:00:13 %ENV: PERL5LIB="/home/hamidjon/Perl/Learning/code_examples/myModules" @INC: /home/hamidjon/Perl/Learning/code_examples/myModules /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl

        I entered this line in a file called 'profile' (/etc/profile) and I can access modules any time without entering each time the line (export PERL5LIB="/home/hamidjon/Perl/Learning/code_examples/myModules"; export PERL5LIB) after the reopening the command line.

Re: using the Modules
by cavac (Parson) on Jul 15, 2012 at 22:12 UTC

    If you unshift @INC, you have to do it in a BEGIN clause before using it.

    Here's an example from my darkpan repo: If you normally install your module in @INC (for example as part of a CPAN distribution), but don't want the hassle while debugging, you can do something like this:

    use strict; use warnings; BEGIN { if(defined($ARGV[0]) && $ARGV[0] eq "--debug") { print("Development INC activated\n\n"); unshift @INC, "/home/cavac/src/maplat_prodit/lib"; # my darkpa +n modules unshift @INC, "/home/cavac/src/maplat_framework/lib"; # my ope +n source modules } else { print("Development INC deactivated, using default INC paths\n\ +n"); } } use Maplat::Worker; use Maplat::Worker::ProdIT;

    "I know what i'm doing! Look, what could possibly go wrong? All i have to pull this lever like so, and then press this button here like ArghhhhhaaAaAAAaaagraaaAAaa!!!"