spacer
Web Development Tutorials CGI-PERL Tutorials
 Developer Newsletter

Tutorials
AJAX
ASP
CGI & Perl
CSS
Flash
HTML
Illustrator
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML
Miscellaneous


Scripts Directory
AJAX Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Web Hosting Directory
ASP.NET
Budget
Dedicated Servers
Ecommerce
Linux
Resellers
Shared
Small Business
Windows

Developer Manuals
Learn HTML
Learn PHP
Learn CSS
Learn AJAX
Learn JavaScript
Learn Pear
Free White Papers

Developer Resources
Developer Tools
Developer Content
Survey Software
Dedicated Servers




Creating Perl Modules for Web Sites

By Tony Lawrence
2005-07-19


Creating Perl Modules for Web Sites

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:

#!/usr/bin/perl5

use MyStuff;
..
newad();
..

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



Tutorial Pages:
» Creating Perl Modules for Web Sites


© Copyright 2005 A.P. Lawrence


 | Bookmark Print |   Write For Us
Related Tutorials:
» Random subroutines in Perl
» Log Script Use
» Bit Vector, Using Perl Vec
» Build a Perl/CGI Voting System
» Perl Range Operator
» Creating Perl Modules for Websites



About the NetVisits, Inc Network | Write For Us | Advertise
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: