lunchbox has asked for the wisdom of the Perl Monks concerning the following question:

Hey guys, I have been programming in C and C++ for about 2 years now, but just recently picked up perl to run the dynamic pages of my website. The problem that I am having, though, is how to build a custom library (in the C sense) of all my commonly used subroutines (ie. record_visit, random_quote, etc.).

As of now, i am just copy and pasting the subroutines into each page that I use them. Not only is this bad because my C instructors would have nightmares looking at my code, but it is annoying to make small changes to the functions and have to do this 20 times every time I find something that is a little bit wrong.

I tried looking at a number of reference sites first but was unable to find a simple answer. I would appreciate the help.

Thanks in advance,

Danny

Replies are listed 'Best First'.
Re: Better Style
by davido (Cardinal) on Sep 23, 2003 at 19:16 UTC
    You are in desperate need of Packages and Modules. From the perldocs, see perlmod. That ought to get you started.

    Since you mentioned that you're using Perl for dynamic web pages (CGI), I'll take this opportunity to once again reiterate that the very first module you probably should consider using while developing CGI scripts is CGI.pm; part of the standard Perl distribution for some time now. While you're at it, the use strict and use warnings pragmas, as well as the -T taint checking command line (or shebang line) option are all 'must-haves' for CGI.

    Hope this helps.

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Re: Better Style
by sauoq (Abbot) on Sep 23, 2003 at 20:08 UTC

    As someone else mentioned, read perldoc perlmod. After that, read perldoc perlmodlib. You might also want to read Simple Module Tutorial, a tutorial written by our very own tachyon.

    Basically though, you'll do something like this:

    package LocalUtilities; require Exporter; our @ISA = qw( Exporter ); our @EXPORT_OK = qw( record_visit random_quote ); sub record_visit { # do stuff... } sub random_quote { # do other stuff... } 1; # Always end your modules with a true expression.
    And then you'll use it like this:
    use LocalUtilities qw( random_quote ); LocalUtilities::record_visit(); print random_quote();
    You can do it without the Exporter stuff, but you might as well use it. Read perldoc Exporter for more information. You should probably also learn about h2xs as soon as possible so read perldoc h2xs too.

    Since you have some C++ background, you will probably be interested in Perl's OO facilities. (You'll either love 'em or hate 'em.) You can find more information by reading the perlboot, perltoot, perltootc, perlobj, and perlbot perldocs and here in the Tutorials section.

    Of course, someone may have already written code that could replace your utility functions; if so, it may even be more complete and better tested than your own. Check CPAN to find out. Finally, after you decide your module fills a niche not already filled by modules available on CPAN (or you think yours does it better), consider contributing your code. Read perlnewmod for some advice on how to do that.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Better Style
by Roger (Parson) on Sep 23, 2003 at 23:49 UTC
    I highly recommend you to read the book "Effective Perl Programming", written by Joseph Hall and Randal Schwartz, if you want to develop a good perl programming habit and style. There are a few book review threads on Perl monks site for this book. The ISBN number is ISBN-0-201-41975-0.

    There is a chapter in the book on Writing Packages and Modules. The method recommended by the author is to use the h2xs program included in your Perl distribution.

    Example:
    To create a new module File::Cmp -
    h2xs -A -X -n File::Cmp
    It will generate the files:
    File/Cmp/Cmp.pm File/Cmp/Makefile.PL File/Cmp/test.pl File/Cmp/Changes File/Cmp/MANIFEST
    These files are a great starting point to further develop your perl module.

    Then in your application, you just add this to the beginning of the perl script:
    use File::Cmp; ....
Re: Better Style
by dsergent (Novice) on Sep 23, 2003 at 19:22 UTC
    You can take all of your related subroutines and create a package. Then all you have to do is call the package via a use statement. Changes would then be made only to the orginal code in the package.
Re: Better Style
by logan (Curate) on Sep 23, 2003 at 21:04 UTC
    On the most basic dumbed-down level: Put all the subs in a single file. Name it my_subs.pm. Make sure that the last line of the file is this:

    1;
    That's a one, not an "L", BTW.

    At the top of your perl script, put use my_subs;

    This isn't an optimal solution by any means, but it will work. From there, you'll want to read the chapters on Packages, Modules, and Objects in the Camel Book.

    -Logan
    "What do I want? I'm an American. I want more."

Re: Better Style
by herveus (Prior) on Sep 24, 2003 at 11:14 UTC
      I second that.

      This book does a good job of helping you 'evolve' your system from a simple script to something more complex.