APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

2005/06/11 Creating Perl Modules for web sites

© June 2005 Tony Lawrence

When you are writing your own code, you are more apt to use someone else's module than write your own, unless your project gets fairly large and complex. Small scripting tasks just don't need the advantages modules offer. However, there is a case where modules might make perfect sense: web server cgi scripts often repeat the same tasks. Putting those common features into a module can make your web scripting easier.

For example, I need to call Google's ad generating code from just about any script that creates a page. I could just hard code it into every script, but suppose I want to change the way it displays or abandon Google entirely and replace the ads with something else? Putting the code in a module other scripts can call gives me that flexibility.

Another advantage to keeping the code separate is that I don't have to worry about clashing variable names: variables I use in the module won't conflict with variables in the calling script and vice versa.

Modules aren't difficult to create:

package MyStuff;
require Exporter;

our @ISA=qw(Exporter);
our @EXPORT=qw(newad midad);
our $VERSION=1.00;

sub newad {

sub midad {

Fill in the code, and that's a working module. I have it in MyStuff.pm, and call it from any script:

use MyStuff;

Modules do have to return a true value; you'll often see them end with "1;" to be sure of that.

That's all it takes. MyStuff.pm does have to be placed somewhere in @INC; I put it at /usr/local/lib//perl5/site_perl/5.8.4/MyStuff.pm

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> 2005/06/11 Creating Perl Modules for web sites

Inexpensive and informative Apple related e-books:

Photos: A Take Control Crash Course

Sierra: A Take Control Crash Course

Take control of Apple TV, Second Edition

Take Control of Numbers

Take Control of Upgrading to El Capitan

More Articles by © Tony Lawrence

Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

C++: NSStringWithoutYourMothersHatButWithNiceCandyFrosting - aargh! (Tony Lawrence)

Linux posts

Troubleshooting posts

This post tagged:


Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode