BW::CGI - Simple OO CGI


  use BW::CGI;
  my $o = BW::CGI->new;


new( [ property => value, ... ] )

Constructs a new BW::CGI object.

Returns a blessed BW::CGI object reference. Returns undef (VOID) if the object cannot be created.

Properties can be set by passing their values in a hash or hashref like this:

  my $o = BW::CGI->new ( content_type => 'text/plain' );

Or by hashref, like this:

  my $properties = { content_type => 'text/plain' };
  my $o = BW::CGI->new ( $properties );

Returns the parsed results of the query string as a hashref, or undef.


Returns a list of query variable names. (q_names is an alias for qnames.)

qv( name [, index] )

Returns the value of the query variable name. If there is more than one variable with the same name a list will be returned, or if index is provided, the value in the specified list position. index is zero-based.


Returns a URI for use as a link back in the form action attribute.

set_status( code [, message] )

Sets the HTTP "Status" code and, optionally, the associated message.

set_cookie( params )

Sets a cookie. Must be called before headers are sent (see set_header). params is a hashref with the cookie parameters: name, value, expires, path, domain, secure, httponly.

get_cookie( name )

Returns the value of the named cookie.

clear_cookie( params )

Clears the specified cookie from the browser by setting an empty cookie. The same parameter rules as in set_cookie apply.

p( string ) print( string )

Prints string to the client. Sends the headers first, if they haven't already been sent.

redirect( destination )

Sends an HTTP redirect (status code 302) to the client with Location set to destination.

set_header( key, value )

Sets header key to value. Must be called before the first call to p (print) as the headers are sent to the client at that time.


Sends the headers that have been set with set_header.

header_date( time )

Returns a header-ish date from a unix-epoch time value.

html_encode( string )

Returns an encoded copy of string with all non-matching /[^a-z0-9_\-\.,?:;\(\)\@! ]/ characters replaced with numeric HTML entities.

url_encode( string )

Returns an encoded copy of string with all non-matching /[^a-z0-9_]/ characters replaced with URL-encoded hexadecimal values (e.g., %20 for space).

url_decode( string )

Returns a URL-decoded copy of string.


Returns and clears the object error message.


Properties can be set or retrieved by using their name as a method, e.g.:

  $o->content_type( 'text/plain' );
  my $ct = $o->content_type;

The available properties for this method are:


The Content-type: header that gets sent to the client.


Value of HTTP_HOST environment variable. Used for creating links back to self, e.g., in the "action" attribute of form.


The maximum content length allowed from POST method queries. Defaults to 1MB (1,0485,776).


Written by Bill Weinman


Copyright (c) 1995-2008 The BearHeart Group, LLC


  2009-11-04 bw     -- added linkback method
  2008-03-26 bw     -- updated and documented
  2007-10-20 bw     -- initial release.