Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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]# ./ Undefined subroutine &main:pen_log called at ./ line + 23.
#!/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 ''; #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 and 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 .

Replies are listed 'Best First'.
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 # use lib '/somewhere/'; require '';
      does this mean, since is not a module, and script, this can be used as script inside of and not necessarily loaded as module?
        use lib '/n2p/cf/lib'; use Logger;

        If 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 # use lib '/somewhere/'; require '';
        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, 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 ./ 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

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://625444]
Approved by randyk
Front-paged by naikonta
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2018-02-25 20:17 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (314 votes). Check out past polls.