TheMACisp
TheMACisp
LOGINWEBMAILSUPPORTSTUFFGOODIESQUICKTIMESERVICES
Back   Perl Support

Perl (Practical Extraction and Report Language)

Back in 1986 Larry Wall needed something better than "awk" to do some file fiddleing and so on December 18th, 1987 Gloria was born, then he changed the name to Pearl and finally shortened it to Perl. Perl is now a very powerful CGI (glue) language and relatively easy to learn. For more in depth information on Perl, click the icon to the right.

Some "Words of Caution" and "Words to Live By"

Perl is a powerful but very literal language and is intolerant of incorrect syntax. Unlike HTML which simply ignores erroneous commands, Perl will crash and more probably never even compile at runtime if you have as little as an accent grave’ out of place

If you don’t know what an accent-grave’ is remember this, every symbol on your keyboard has a name, (get to know them) and in Perl almost all of them are some form or sub form of a Perl command including the invisible ones like control characters.

The way you edit and download/upload a Perl scripts is very important and needs to be done in a way so you do not strip or add any of those pesky invisible characters like ^M’s (control-M) ,(carriage return). Having one of these invisible goodies at the end of each line in your script is a reason to not compile as far as Perl is concerned, while HTML and some other languages could care less and would continue unabated.

To avoid this embarrassment, force your FTP client to ASCII mode before doing a download or upload and always save your edits as a plain text files only.

Perl script must reside in the "cgi-bin" directory located in your websites root directory. Perl scripts can reside in any user defined directories inside of you cgi-bin directory to any depth you may need.

Perl script files must use the “.cgi” extension for their filenames.

Perl scripts must be enabled for execution by setting their mode (chmod) to 755.

Perl scripts must use “#!/usr/bin/perl” as their first line. This is the location of the Perl Interpreter.

Perl scripts must "\" (escape) all "@" (at signs) in any text portions such as in the text of an email address. The "@" is a reserved character used to signify arrays in Perl.

The Perl-ized HTML page.

The following code is a simple Perl script that generates an HTML webpage. These basic principles and techniques apply to most Perl scripts in one way or another.

#!/usr/bin/perl

print “Content-type: text/html\n\n”;
print <<END_OF_HTML;

<HTML><HEAD><TITLE>Simple Webpage</TITLE></HEAD>
<BODY>
This is a simple webpage generated by a Perl script.
</BODY>
</HTML>

END_OF_HTML

The first line points the script to the Perl interpreter.
This line must always be the first line in your Perl Scripts which locates the Perl Interpreter on our server for this server script.
The second line sets the ouptut mode to HTML.
This line is the HTTP header and can only be executed once in any given script. A HTTP header can contain functions other than the Content-type which sets the STDOUT for the specified type of output. You can stack header functions like this for example which sets a cookie and then launches a new server script:

print "Set-Cookie: $cookievalue\n";
print "Location: index2.cgi\n\n";


HTTP headers are a subject unto themself and deserver more scrutiny that can be presented here. Refer to the O'Reilly CGI Programming with Perl book to discover the awe and mystery of the HTTP header in all its glory.
The third and the ninth lines form the HTML encapsulation.
This is a really neat trick and saves hours of having to put in print statements, quotes, escapes and semi-colons to make your HTML code presentable to the Perl Interpreter. The O'Reilly Programming Perl book says it better than I:

"Here" documents... A line-oriented form of quoting based on the shell's here-document syntax. Following a << you specify a string to terminate the quoted material, and all lines following the current line down to the terminating string are quoted.

This technique will be called on time and time again, be it for making HTML or making emails or any other specific data structures.
All the other lines are plain old HTML.
You can substitiute Perl variables for words or literally anything in the HTML string and instantly you have create a dynamic webpage. This is the fundamental distillation and essence of dynamic webpages, of course most are a bit more complex than this example.

Packages and Modules

Currently we have the following Perl packages/modules installed for your use: Check List. Other packages or modules you need may be installed depending on systems security and reliability, Check by calling tech support at: 1-800-337-0663.

Installing My Own Module

We do not recommend installing your own modules @INC directory installation is blocked so to install a module "of your own" you need to declare a place for it (and put it there).

Include lines like the following in any Perl script that requires your module, in this example lets use: MYmodule.pm and we'll put it in your site directory in a subdirectory called includes.

use lib /Users/your initial/your username/Sites/yourwebsite/includes/;
use MYmodule;


If your username is snappy and your website is sammysmoot.com then the lines would read:

use lib /Users/s/snappy/Sites/sammysmoot.com/includes/;
use MYmodule;

It should be noted that installation of this type is limited by your level of permissions on the server. Not all modules will install with this restriction.

A Few Good Chunks of Perl?

These are a few program segments in Perl that rely heavily upon another language for their magic. This language is called Regular Expression which is built into most powerful languages. It's all symbol based rather than word based and thus looks like gibberish

The POST String Decoder
This code is invaluable when working with forms that use the POST method. Put the URL of a Perl script containing the following code in your forms ACTION statement.This code will put the POST string information from your form in an associative (hash) array as name value pairs called $FORM.

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs){
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $FORM{$name} = $value;
}

The Comma Subroutine
This subroutine will insert commas in large number strings every 3 digits ascending. A must for the shopping cart or accounting program you are currently writing

sub commify {
    my $text = reverse $_[0];
    $text =~ s/(\d\d\d)(?=\d)(?!\d*\,)/$1,/g;
    return scalar reverse $text;
}

This is a way of using commify to represent money, there are more ways to use it, those are up to you.

$total = commify(sprintf("%.2f",$total));

The Duplicates Remover
This one-liner removes any duplicates in an array where @list is the input array with the duplicates and @result is the output array where the duplicates have been removed

%seen = ();@result = grep { ! $seen{$_} ++ } @list;

These are only a few examples of the more advanced Perl programming possible, I didn't even want to get into the Schwartzian Transform (which is really neat too), but if you want to, a good reference is O'Reilly's Perl Cookbook.



56K Dialup | OSX Hosting | 56K Dialup + | Numbers | Status | Support | Login | Webmail | Streaming Dead
Streaming Phish | Hitchikers... | Time Warp | Contact | Logos | Agreements | Policies

© 2000-2009 So What Software, Inc. This site is served on OSX server and was created using Adobe® GoLive 6. Mac, Apple, iChat, WebObjects and Quicktime are registered trademarks of Apple Computer Inc. GoLive is a registered trademark of Adobe Systems Incorporated. Dreamweaver is a registered trademark of Macromedia, Inc. Internet Explorer, Outlook Express and Entourage are registered trademarks of Microsoft Corporation. Eudora is a registered trademark of QualComm Corporation. MySQL is a registered trademark of MySQL AB. Netscape is a registered trademark of Netscape Communications Corporation.