in reply to Re^2: Require or Do vs. more maintenance
in thread Require or Do vs. more maintenance

No, you don't need to worry about that. Libraries can live anywhere. You just want to add:
use lib qw( /path/to/my/libraries/ );
At the top of your scripts (or in the initialization file for your web server), to tell perl where to look for libraries. Also, don't let the use scare you. It's built on the same mechanism as require, basically, there's just some more tricks involved. One consequence of those differences, though, is that you have to use module-names (where the path-separator is "::" and the path is relative to the library path(s)) with a use statement, whereas with require, you can use either a module-name or a file-name

The two things that are different between use and require are that

This means that use Foo::Bar; is actually equivalent to just:
BEGIN { require Foo::Bar; Foo::Bar->import(); }
Of course, if your module doesn't have an import method defined, nothing bad happens, because (since it's a method call, not a function call) it ascends up the class hierarchy to UNIVERSAL::import (...but I'm getting into details, here).

Look at perldoc perlmodlib perldoc perlmod for more info. And good luck!

------------ :Wq Not an editor command: Wq

Replies are listed 'Best First'.
Re^4: Require or Do vs. more maintenance
by Stenyj (Beadle) on Jun 26, 2004 at 22:27 UTC
    *acts like a sponge and absorbs everything*

    Alright, thx! Been trying to get it to work, without luck though.

    test.cgi
    #!c:/apache/perl/bin/perl.exe BEGIN { $| = 1; open (STDERR, ">&STDOUT"); print qq~Content-type: text/html\n\n~; } use CGI; use strict; use lib qw( c:/apache/cgi-bin/lib ); use MyAppCommon; # use is basically like require... see the docs for my $foo = new CGI; print $foo->header; MyAppCommon::top(); print "Test content (should be in middle)<br><br><br><br>\n"; MyAppCommon::bottom();


    MyAppCommon.pl (.pl a necessary extension? or can it be .cgi?)
    use strict; sub top { print "Top stuff<br><br>"; } sub test { print "Bottom stuff<br><br>"; } 1;

    Assuming the code above, where am I suppose to save MyAppCommon.pl? Does it need to be compiled somehow? I know I could put it in the bin on my test environment, but I want to mimic my website's environment as best possible, so would like to have it in a subdirectory of my cgi-bin.


    Thx again for all the help, time, and patience! I'm learning a ton tonight :-)


    Stenyj
      Ah, sorry I wasn't specific. Perl module files should be saved with a ".pm" extension, and they should be saved in the library path (really, in a library path, as there may be multiple... when you require or use a module, all the library paths in @INC are searched for that file).

      Also, module-names map to file-names in the following manner: two colons ("::") are used to separate files from directories or directories from sub-directories (regardless of what is actually used for this purpose on your operating system: backslash for windows, slash for *nix, colon for mac classic, etc). So, require My::New::Library; is equivalent to require "$lib_path/My/New/Library.pm"; on *nix, or to require "$lib_path\\My\\New\\Library.pm"; on dos/windows, etc.

      Again, though, I'm just parroting back documentation to you (not that I mind). You can see this stuff for yourself in perldoc perlmod. (I think before I mentioned perlmodlib, but I should have checked that first... I actually had it mixed up with perlmod, which is more useful info for what you're trying to get a handle on... sorry).

      ------------ :Wq Not an editor command: Wq
        use lib qw( c:/apache/cgi-bin/lib );

      You are telling Perl to look for "MyAppCommon" into "c:/apache/cgi-bin/lib".

      MyAppCommon.pl (.pl a necessary extension? or can it be .cgi?)

      Modules use the ".pm" extension.