in reply to Require or Do vs. more maintenance

Ultimately the way to do this is to make a library for the start and finish pieces, and make subroutines for them.
package MyAppCommon; use strict; sub CommonPageStart { ... } sub CommonPageEnd { ... } 1; __END__ # and then, in your scripts use MyAppCommon; # use is basically like require... see the docs for +details MyAppCommon::CommonPageStart(...); # your stuff MyAppCommon::CommonPageEnd(...);
This is the "right" way to do it for lots of reasons... not the least of which is that this is the way that most things work, and you will have an easier way of integrating other people's work with yours, as well as having your work be more easily understandable to someone else (if you hire help or if you are asking for help on perlmonks :-D).

As for more direct reasons of why this is better:

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

Replies are listed 'Best First'.
Re^2: Require or Do vs. more maintenance
by Stenyj (Beadle) on Jun 26, 2004 at 20:56 UTC
    Alright, that sounds good and makes sense. Question though. I'm hosted on a shared hosting.

    To make the library, is it just a matter of creating the file with the content you have above, or do I have to do something more specific where I would require higher access to the server (other then FTP)?

    If I need access to the server (which I can't get) to create a library like you suggested, would 'require' be a equally decent alternative?

    Thx for all the feedback guys! I always learn a ton when I ask for input here. It's greatly appreciated!


    Stenyj
      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

      • use happens at BEGIN-time (that is, as the code is being parsed and compiled, rather than later on as the code is being executed). This means, for example, that:
        if (0) { use Some::Module; }
        will still use the module!
      • use also automatically calls the import method of whatever module it is loading, and you can specify parameters to that import method after the use statement. (Be careful, though, because since the use is executed earlier than it looks like in the code, passing variables into the import method could cause you hard to understand results... generally just static arguments are passed to the import method this way). For example, look up the page at where I said use lib qw( /path/to/my/libraries/ );, that actually means that lib->import( '/path/to/my/libraries/' ); is getting called (lib being a standard core module for manipulating the directory-path).
      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
        *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