Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
The stupid question is the question not asked
 
PerlMonks  

try to understand use lib in module

by convenientstore (Pilgrim)
on Jul 07, 2007 at 21:53 UTC ( #625444=perlquestion: print w/ replies, xml ) Need Help??
convenientstore has asked for the wisdom of the Perl Monks concerning the following question:

Hello first timer here posting~ Trying to get a better grip on perl module and,I was testing out below script, When I use "use Acme statement", it loads the module properly and it works. However, when I want to test out the ' use lib ' it comes up with below error message(looks like it is able to locate but cannot read or import back the function for whatever reason??).
[root@myserver perl]# ./logtest2.module.pl Undefined subroutine &main:pen_log called at ./logtest2.module.pl line + 23.
Code:
#!/usr/bin/perl -w use strict; # # Below use Acme works ; just uncomment it # #use Acme::Webserver::Logger; # # See if you can understand and do use lib # use lib '/usr/lib/perl5/site_perl/5.8.8/Acme/Webserver'; require Logger; #require 'Logger.pl'; #use Logger; # /usr/lib/perl5/5.8.5/Acme/Webserver open_log("webserver.log"); write_log(1, "A basic message"); write_log(10, "A debugging message"); log_level(10); write_log(10, "Another debugging message"); close_log();
I have both Logger.pm and Logger.pl(just in case) in usr/lib/perl5/site_perl/5.8.8/Acme/Webserver
DB<1> print "@INC"; /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/ +site_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8. +6/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5/i386-linux-t +hread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl/5 +.8.7 /usr/lib/perl5/site_perl/5.8.6 /usr/lib/perl5/site_perl/5.8.5 /u +sr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-th +read-multi /usr/lib/perl5/vendor_perl/5.8.7/i386-linux-thread-multi / +usr/lib/perl5/vendor_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl +5/vendor_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/vendor_per +l/5.8.8 /usr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5/vendor_perl/5 +.8.6 /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl /usr +/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .

Comment on try to understand use lib in module
Select or Download Code
Re: try to understand use lib in module
by runrig (Abbot) on Jul 07, 2007 at 22:01 UTC
    'use lib' alters the search path for finding modules, it does not import modules. see 'perldoc lib'. 'use ModuleName' imports module 'ModuleName', as you were correctly doing.
      Ok, thanks, I am trying to understand this,
      use lib '/n2p/cf/lib'; use Logger;
      Does work. but this is confusing. Someone at work wrote a script with
      #!/usr/bin/perl -w # file1.pl use lib '/somewhere/'; require 'somefile.pl';
      does this mean, since somefile.pl is not a module, and script, this can be used as script inside of file1.pl.. and not necessarily loaded somefile.pl as module?
        use lib '/n2p/cf/lib'; use Logger;

        If Logger.pm is in /n2p/cf/lib, and /n2p/cf/lib is not in @INC, then that makes sense, or if something else is being imported from that directory. If /n2p/cf/lib is a common directory to import libs from, then you might want to put it in the PERL5LIB environment variable, and get rid of the 'use lib'.

        #!/usr/bin/perl -w # file1.pl use lib '/somewhere/'; require 'somefile.pl';
        This style was common in perl 4 to import libraries (as that's all there was to do the job), so if someone is still writing this way, they should probably be updated, and if possible, somefile.pl should probably be packaged as a proper module. But require takes either a bare word which imports a module, or a string (as in the above example) which imports a specific file name.
Re: try to understand use lib in module
by northwind (Hermit) on Jul 07, 2007 at 22:27 UTC

    Notice the error message.  Read the error message.  Believe the error message.

    Undefined subroutine &main:pen_log called at ./logtest2.module.pl line 23.

    In particular, I noticed you are using a function by the name of open_log().  In the error message, Perl is complaining about not finding a function by the name of pen_log().  Do you see the problem now?

    Also, read what runrig wrote as well as the perldoc on Perl Modules.

Re: try to understand use lib in module
by Anonymous Monk on Jul 07, 2007 at 23:31 UTC
    thank you guys!! I think I have better understanding now of what I was seeing at work.. off to more reading!! thanks big time~
Re: try to understand use lib in module
by sen (Hermit) on Jul 09, 2007 at 13:30 UTC
    hi

    Please, see this node praveen, it gives more idea of lib.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://625444]
Approved by randyk
Front-paged by naikonta
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2014-04-18 10:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (466 votes), past polls