Name
cgi::pk_cgi - Lightweight Perl module for handling the most common CGI functions. Simple to use, single file with no dependencies, and short learning curve for those times when you don't want or need the swiss army knife of cgi modules.
Synopsis
use cgi::pk_cgi;
my $Page = pk_cgi->new_page("html");
my $Cookie = {Name => "pklogin", Value => {UserName => $Self->{Env}{username}, Password => $Self->{Env}{password}}; $Page->add_cookie($Cookie);
%PKEnv = pk_cgi::cgi_request() or die "No Http Environment";
%Cookies = pk_cgi::get_cookie_list() or die "no cookies"; $SomeCookie = $Cookies{SomeCookieName}; $SomeCookieValue = $$SomeCookie(SomeCookieVariable};
my $Html = "<html><head><head><body>hello world!!</body></html>"; $Page->append_to_page($Html);
$Page->print_page();
Description
This module handles all the details of sending a document of any content type to a WebBrowser. It also handles retrieving form data from sent via "get" or "post" in regular or multipart/form-data encodings(file uploads). It does this transparently to the user through a single interface. Finally it handles setting and retrieving cookies. It does all this as a standalone module with no dependencies for an easy install.
The following methods are are available for use in this module.
class methods
- cgi_request()
-
Retrieves the form data. It automatically detects the send method and the encoding. The return value is a hash with the variable names as keys. In the case of file uploads the value is another hash with the file information and contents. The file hash contains the following keys (name, content_type, file).
-
Retrieves a cookie by name. This method only reliably decodes cookies which were set by this script. It returns a hash of the variables stored in the cookie. The variable names are the keys of the hash. The method expects to a string containing the name of the cookie you wish to retrieve.
-
Retrieves all the cookies sent by the Browser application. It returns a hash of all the cookies. The keys of the hash are the names of the cookies. And the values of the keys are hashes of all the variables in the cookie.
A note on single value cookies
In the hashes returned for single value cookies the variable name in the hash is the same as the name of the cookie.
object methods
- new_page()
-
The constructor for the cgi object. There is an optional string argument which specifies the mime type of the document html, xml, plain, and so on. IT should be called with the following syntax. pk_cgi->new_page("html");
-
Sets a cookie in the document header. This function must be called before the print_page function or the cookie will not be sent. It expects a hash as an argument in the following format: {Name => "name", Value => "value"} where Value can be either a hash of values stored under there variable names or a single scalar. You may also include the following optional keys in the hash Expires, Path, and Domain each one corresponding to their conterpart in a cookie.
- append_to_page()
-
Appends a string to the bottom of your document. It is used to build your page before sending with the print_page method. It expects the string you wish to append as an argument.
- prepend_to_page()
-
This is the same as the append_to_page method except it adds the string to the beginning of your document. It also expects the string you wish to append as an argument.
- get_header()
-
This can be used if you only wish to use the header building functionality. It retrieves only the headers for your document. It is probably not of much use except in debugging perhaps.
- get_page()
-
This retrieves your whole document including the headers. you can pring the returned string to send your document to the Browser. Or you can use the print_page method instead to automatically print it.
- print_page()
-
This sends your built page and any cookies set beforehand to your browser. This should be your last act since once called the page cannot be changed and no more cookies can be set.
Version
0.4 Written 02-04-2004
Author
(c) 2003 Jeremy Wall <Jeremy@marzhillstudios.com> http://jeremy.marzhillstudios.com
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
PREREQUISITES
This script requires the strict
module. It also requires the Exporter
nmodule
Any
CGI