Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Install My Own Package 1

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
US
How do I go about installing my own package? I created a package (.pm). It's a very simple package. Do I need to do something special to install it? Can I simply make a sub-directory in perl_root and put it in there?
 
u can use require or do perl -V to check where the other modules are located and place it there.
 
This is how modules work:

Our guinea pig will be LWP::UserAgent. You have the line "use LWP::UserAgent" in your code. Perl converts the module name into something that loosk like a file path (the :: becomes a / and a .pm is tacked onto the end, so LWP::UserAgent becomes "LWP/UserAgent.pm").

It then loops through the directories in @INC and, for each directory, it takes on LWP/UserAgent.pm to the end. As soon as it finds that such a file exists, it stops searching and includes that module.

So if @INC contains this:
Code:
@INC = (
   'C:/Perl/lib',
   'C:/Perl/site/lib',
   '.',
);

then Perl will search for these files:
Code:
C:/Perl/lib/LWP/UserAgent.pm
C:/Perl/site/lib/LWP/UserAgent.pm
./LWP/UserAgent.pm

If it doesn't find one, it will say "Can't locate LWP/UserAgent.pm in @INC (@INC contains: C:/Perl/lib; C:/Perl/site/lib; .)"

Having said that, just put your module in a place where Perl will find it giving it a file name and path that matches the module's name. As a tip: you don't have to put it in the main Perl module paths, you can place it in a folder that's next to your Perl script. So if you have a folder named "mymodules" in the same place as your Perl script, you do...

Code:
use lib "./mymodules";
use My::Custom::Module;

And that'll try to load "./mymodules/My/Custom/Module.pm" before searching the rest of @INC (the "use lib" module essentially puts your own custom search paths onto the front of the @INC array; you can override installed modules with custom versions this way).

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Thanks, Kirsle, that's a nice simple, clear explanation.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top