Using CPAN modules to save coding time/complexity, namely CGI.pm

 

The best way to write computer programs is by letting someone else do the hard work.  Some might call it laziness, but I call it efficiency.  Why reinvent the wheel?  Cpan.org is the largest source of pre-written code for your perl/cgi programs, and all the modules there are free!

 

We will cover the most important CPAN module for CGI, called simply “CGI”, or “CGI.pm” (the “.pm” stands for “perl module”).  However, I urge you to look at cpan.org and see if there’s anything that would assist you with writing programs you write.

 

CGI:

 

CGI has several very useful built in functions that are available when you say:

 

Use CGI qw(:standard);

 

At the beginning of your cgi script.  They are as follows:

 

header

 

Use like this:

 

print header;

 

This prints the required HTTP header, which actually consists of this text (or similar): “Content-type: text/html\n\n”

 

param

 

A great function, this gives you the value of one of the name/value pairs inputted to your cgi script.  When called without a name of a name/value pair, it returns an array of all name value pairs.

 

For Instance:

 

my $email_address = param(‘email_address’); # easy!

my @all_name_value_pairs = param; # also easy!

 

redirect

 

Allows you to redirect users to a specific URL (web site address):

 

For Instance:

 

print redirect(‘http://www.yahoo.com’); # takes user to yahoo.com

 

Note, this only works if you have not yet printed your http header.

 

url

 

This gives you the full URL of the currently running script.  It’s useful in self-referencing forms, which you’ll see below in the examples section.

 

For Instance:

 

my $url = url;

 

HTML Markup:

 

If you like, you can use CGI.pm to generate your html markup.  Almost all html version 3 tags are supported.

 

For instance, this code:

 

my $url = url;

print header,

start_page,

h2(‘Simple Form’),

start_form(-action=>$url,

                -method=>’post’),

‘your name: ‘,

textfield(‘your_name’),

br,

‘your email address: ‘,

textfield(‘email_address’),

submit(‘Click here to show name and email!’),

end_form,

end_page;

 

will print out a form nearly identical to the form shown above.

 

In the above code, take a closer look at this section:

 

start_form(-action=>$url,

           -method=>’post’),

 

 

Not only is this taking advantage of perl’s ability to span a print statement over multiple lines by using commas, it’s also showing that some particular functions take input in a slightly different manner than others.  Instead of typing:

 

start_form($url, ‘post’),

 

Like we talked about before, this code uses what are called the ‘hash input syntax’.  This sounds a little scary at first, but it’s not really so bad.  All it means is that we are passing a hash to a function as it’s arguments (input).  Remember before when we talked about hashes (also called “associative arrays”), that these are simply name value lists.

 

So we could write the above code like this:

 

my %start_form_arguments;

$start_form_arguments{‘-action’} = $url;

$start_form_arguments{‘-method’} = ‘post’;

start_form(%start_form_arguments),

 

The CGI.pm module requires you put a dash in front of the names in the name value pair hash, but not all modules require this.  You will need to check the instructions that come inside each module, which are embedded inside the file (perl module files end with a “.pm” file name extension) via POD, which stands for “Plain Old Documentation”.

 

Cookies:

 

Cookies are small text files that you can tell the browser to save to your user’s hard drive, enabling you to save small bits of information for use either when they visit your site later, or during a different request to your server during the current “session”.

 

To set a cookie, you first need to define a name, a value, a path, a domain, when the cookie expires, and if the cookie should only be read when your server is using SSL (encrypted request for a page).

 

The name and value are just like the name value pairs we’ve mentioned above in both hashes and the name value pairs that html forms send your cgi scripts.  The path is what part of your site the cookie is active on, for instance setting it to /this_directory_only will not let you read it if you are at the root level of your site.  The domain is your domain name.  The easiest way to do this is like so (assuming your domain is “my-domain.com”) .my-domain.com.  This puts a period before your domain name, but not after.  Don’t include the machine name for your domain (for instance, “www”) in your cookie.  The next thing is when your domain expires.  CGI.pm has a cool utility to format the date for you, which you should use and looks like this: +1h for one hour, +1d for one day, +1y for one year, and so on.  You usually won’t need to have the cookie only read over an SSL (“Secure Sockets Layer”, encryption used for ecommerce and other sensitive data) connection, but if you do you set this part to “1”.

 

So to set a cookie with CGI.pm, use this syntax:

 

$cookie = cookie(-name=>'name',
                 -value=>'value',
                 -expires=>'+1y',
                 -path=>'/',
                 -domain=>'.your-domain.com',
                 -secure=>0);
print header(-cookie=>$cookie);

 

Make sure to do it like it is above, where you create a cookie “object” (don’t worry about what an object is for now), then set that object called $cookie as a parameter to the header function.

 

To retrieve the value of a cookie you have set previously, do this:

 

my $cookie_value = cookie('name');

 


<-- Previous: What is CGI, and what do I need to know about it to program in it? | Next: CGI Scripting methodology, the application mode�s approach -->


Please rate this cgi tutorial on cgi-resources.com:

CGI-Resources Rating:


Copy the Shrug Emoji
Ecommerce Shopping Cart Software
ShopCMS Paypal Shopping Cart
Free CGI Scripts
CGI Tutorial
Software Engineering Consultant
Search Engine Optimization Tips
How To Choose Quality Web Hosting
Free Search Engine Ranking Software
HTTP Compression
Install CGI Scripts
Tell A Friend Script
LittleFTP Free FTP Client For Windows


Link To
This Page!


Copy the following code and paste it into your html file.
Ecommerce Shopping Cart Software | ShopCMS Paypal Shopping Cart | Software Engineering Consultant | Free Search Engine Ranking Software | HTTP Compression | Install CGI Scripts | Search Engine Optimization Tips | CGI Tutorial | CGI Scripts | How To Choose Quality Web Hosting | Tell A Friend Script