Programming Perl scripts for Linux and Win32
If your new to Perl, then you should start as you
mean to go on. If you are already a Perl programmer, then
this guide should (unless you're really good) help you improve
your skills and scripts.
Perl can run on both Linux (Unix) systems and Windows (Win32).
Perl can also run as ASP (PerlScript) on Win32 IIS. Most
programming guides for Perl will teach you how to write
scripts for Linux. You then later have to update your code
to work for Win32 from another guide. It's all a lot easier
if you code your script for Linux and Win32 in the first
place. This guide will give you the template for coding
universal scripts that can run as .pl, .cgi or .asp or if
you're running Apache, '.anything'. It will also show you
how to make your database enabled programs work with MySQL,
MS SQL (ODBC) or ADO. In my opinon this is the future of
how all perl web applications should be written. By Perl
web applications I mean those such as on my site CosmicScripts.com,
if you want your scripts to be usable by as many people
as possible, on as many servers as possible then follow
Here is the code you should use to start all your scripts.
What does this code do? It flushes the buffers, checks which
operating system your on, gets the system path to it's installed
folder and adds this path to perl @INC modules folder list
and sets this as the working directory, gets the name and
extention of the itself, checks if it is running in ASP,
loads CGI::Carp for friendly error handling, loads our free
restricted Cosmic module, loads the sendmail module if on
Win32, loads the ASP module if in ASP mode and populates
Perl's standard ENV variable with the ASP environment variables.
The part of this code below the commented #Script Start#
will be described later. This leaves you with some useful
variables to work with:-
- This is set to 1 if the program is running as ASP, otherwise
- This is set to 1 for Unix and 0 for Win32.
- This is set to the full system path to the scripts working
- This is the name of the script without the extention.
- This is set to the working scripts extention, be that
.pl, .cgi or .asp.
- This is set to a readable friendly description of the
- This is kept for backward compatability, and set to
the operating systems path separator. Perl for win32 now
uses / instead of the double \\.
For this code to work you will need the modules ASP.pm
Perl Pages thanks for Matt for a very useful script.
You also need SendMail.pm
Tneoh Chee-Boon and our own slimmed down Cosmic.pm
module. Why have I chosen these modules instead of others
from CPAN? I'm always impressed when a module doesn't have
to be installed into perl with make or ppm. If you are making
commercial or highly distributed scripts you can't expect
everyone to install some obscure perl module. Even if they
want to most people are on shared hosting accounts, with
very incooperable hosting companies. These modules just
need to go in the same folder as your script and you can
use them at will.
The code below the commented #Script Start# is also useful.
Firstly it prints the Text/HTML content type header if it's
not in ASP mode. It then populates the variables %FORM and
%QUERY with hashed values from query string and posted form
data. Lastly it checks for ASP mode and populates %FORM
in the way needed when running in ASP mode.
Now you have a good starting framework for writing a versatile
script. The only problem is that running in ASP mode you
need to have:-
<%@ Language=PerlScript %>
..... All script code here
Around your scripts. So if people want to run your scripts
as .asp they'll need to add this code. But there is a solution,
I'll be bringing you an application you can use to include
in with your scripts that will ask the end user which mode
they want to run the scripts, .pl, .cgi or .asp, and adjust
the script files appropriately for them :) I'm currently
deciding on which program would be best to make this utility
in. At the moment NSIS from NullSoft is in the lead, I think
it will provide everything I need. As soon as I get a bit
of time I'll get started on this project. I'm looking forward
to it :)
The Cosmic.pm module
If you've installed any of my scripts you'll see that they all come with a
module called cosmic.pm. This is my module I have built up over the years for
doing all kinds of stuff. Unfortunately I'm not (as yet) going to make it avilable
for everyone to use. But I am however going to put together a slimmed down version
with some routines I'm sure you'll find useful. Of course this module doesn't
need to be properly installed or compiled into perl, you can just throw it in
with your scripts.
I'm not going to write you a full guide on SQL or doing
database work. There are plenty. I am however going to show
you how to setup a database connection for MySQL, MS SQL
(ODBC) and ADO (ASP only). What about the syntax differences
between MySQL, SQL and MS SQL SQL? (lots of SQL there, lol)
Code your SQL syntax for MySQL, then use the module I'm
developing MytoMSSQL.pm which will translate your SQL to
MS SQL. Here is the current state
of this module. If you want to make use of it feel free
to contribute and email me with new rountines that you add.
Once this module is built up it'll be heading for CPAN.
the latest news?
Then why not sign
up for our newsletter? Be notified immediately about new
products, upgrades, sales, discounts, bug fixes, site updates