[Omaha.pm] Using a perl module without installing it...

Jay Hannah jay at jays.net
Fri Jan 14 12:53:36 PST 2005


On Jan 11, 2005, at 2:58 PM, Daniel Linder wrote:
> Questions:
> 1: Can anyone give me pointers so that I can replace the "use 
> XXX::YYY;"
> syntax and do something like "include /full/path/to/YYY.pm" or other 
> such
> trick?
>
> 2: Or, can someone give me hints as to how to write my script to modify
> the @INC variable (??) to use ~/lib rather than the systems default
> "/usr/lib/perl5/..." directories?


So I have a package "Jay" in a weird location:


$ pwd
/Users/jhannah/weird_location
$ cat Jay.pm
package Jay;

sub hello {
    print "Hi!\n";
}

1;


If I'm in /Users/jhannah and I try to use it it doesn't work:


$ perl -e 'use Jay; Jay::hello()'
Can't locate Jay.pm in @INC (@INC contains: /sw/lib/perl5 
/System/Library/Perl/5.8.1/darwin-thread-multi-2level 
/System/Library/Perl/5.8.1 
/Library/Perl/5.8.1/darwin-thread-multi-2level /Library/Perl/5.8.1 
/Library/Perl /Network/Library/Perl/5.8.1/darwin-thread-multi-2level 
/Network/Library/Perl/5.8.1 /Network/Library/Perl .) at -e line 1.
BEGIN failed--compilation aborted at -e line 1.


(Solution 1)

        Use -I when invoking perl

        from "perldoc perlrun"
        -Idirectory
             Directories specified by -I are prepended to the search 
path for
             modules (@INC), and also tells the C preprocessor where to 
search
             for include files.  The C preprocessor is invoked with -P; 
by
             default it searches /usr/include and /usr/lib/perl.

$ perl -I "/Users/jhannah/weird_location" -e 'use Jay; Jay::hello()'
Hi!


(Solution 2)

        Set your $PERL5LIB environmental variable.

        from "perldoc perlrun"
        PERL5LIB    A list of directories in which to look for Perl 
library
                    files before looking in the standard library and the 
cur-
                    rent directory.  Any architecture-specific 
directories
                    -snip!-


$ setenv PERL5LIB /Users/jhannah/weird_location
( That was MAC OSX. Linux would be 'export 
PERL5LIB=/Users/jhannah/weird_location' )
$ perl -e 'use Jay; Jay::hello()'
Hi!


(Solution 3)

        'use lib' in your script.
        see "perldoc lib"

$ perl -e 'use lib "/Users/jhannah/weird_location"; use Jay; 
Jay::hello()'


HTH,

j



More information about the Omaha-pm mailing list