"there lived a minotaur named",
strong("Fred."),
),
hr;
=head2 Providing arguments to HTML shortcuts
The HTML methods will accept zero, one or multiple arguments. If you
provide no arguments, you get a single tag:
print hr; #
If you provide one or more string arguments, they are concatenated
together with spaces and placed between opening and closing tags:
print h1("Chapter","1"); # Chapter 1
"
If the first argument is a hash reference, then the keys
and values of the hash become the HTML tag's attributes:
print a({-href=>'fred.html',-target=>'_new'},
"Open a new frame");
Open a new frame
You may dispense with the dashes in front of the attribute names if
you prefer:
print img {src=>'fred.gif',align=>'LEFT'};
Sometimes an HTML tag attribute has no argument. For example, ordered
lists can be marked as COMPACT. The syntax for this is an argument that
that points to an undef string:
print ol({compact=>undef},li('one'),li('two'),li('three'));
Prior to CGI.pm version 2.41, providing an empty ('') string as an
attribute argument was the same as providing undef. However, this has
changed in order to accommodate those who want to create tags of the form
. The difference is shown in these two pieces of code:
CODE RESULT
img({alt=>undef})
img({alt=>''})
=head2 The distributive property of HTML shortcuts
One of the cool features of the HTML shortcuts is that they are
distributive. If you give them an argument consisting of a
B to a list, the tag will be distributed across each
element of the list. For example, here's one way to make an ordered
list:
print ul(
li({-type=>'disc'},['Sneezy','Doc','Sleepy','Happy'])
);
This example will result in HTML output that looks like this:
This is extremely useful for creating tables. For example:
print table({-border=>undef},
caption('When Should You Eat Your Vegetables?'),
Tr({-align=>'CENTER',-valign=>'TOP'},
[
th(['Vegetable', 'Breakfast','Lunch','Dinner']),
td(['Tomatoes' , 'no', 'yes', 'yes']),
td(['Broccoli' , 'no', 'no', 'yes']),
td(['Onions' , 'yes','yes', 'yes'])
]
)
);
=head2 HTML shortcuts and list interpolation
Consider this bit of code:
print blockquote(em('Hi'),'mom!'));
It will ordinarily return the string that you probably expect, namely:
Hi mom!
Note the space between the element "Hi" and the element "mom!".
CGI.pm puts the extra space there using array interpolation, which is
controlled by the magic $" variable. Sometimes this extra space is
not what you want, for example, when you are trying to align a series
of images. In this case, you can simply change the value of $" to an
empty string.
{
local($") = '';
print blockquote(em('Hi'),'mom!'));
}
I suggest you put the code in a block as shown here. Otherwise the
change to $" will affect all subsequent code until you explicitly
reset it.
=head2 Non-standard HTML shortcuts
A few HTML tags don't follow the standard pattern for various
reasons.
B generates an HTML comment (). Call it
like
print comment('here is my comment');
Because of conflicts with built-in perl functions, the following functions
begin with initial caps:
Select
Tr
Link
Delete
Accept
Sub
In addition, start_html(), end_html(), start_form(), end_form(),
start_multipart_form() and all the fill-out form tags are special.
See their respective sections.
=head2 Autoescaping HTML
By default, all HTML that is emitted by the form-generating functions
is passed through a function called escapeHTML():
=over 4
=item $escaped_string = escapeHTML("unescaped string");
Escape HTML formatting characters in a string. Internally this calls
L (encode_entities) so really you should just use that
instead - the default list of chars that will be encoded (passed to the
HTML::Entities encode_entities method) is:
& < > " \x8b \x9b '
you can control this list by setting the value of $CGI::ENCODE_ENTITIES:
# only encode < >
$CGI::ENCODE_ENTITIES = q{<>}
if you want to encode B entities then undef $CGI::ENCODE_ENTITIES:
# encode all entities
$CGI::ENCODE_ENTITIES = undef;
=back
The automatic escaping does not apply to other shortcuts, such as
h1(). You should call escapeHTML() yourself on untrusted data in
order to protect your pages against nasty tricks that people may enter
into guestbooks, etc.. To change the character set, use charset().
To turn autoescaping off completely, use autoEscape(0):
=over 4
=item $charset = charset([$charset]);
Get or set the current character set.
=item $flag = autoEscape([$flag]);
Get or set the value of the autoescape flag.
=back
=head1 CREATING FILL-OUT FORMS:
I The various form-creating methods all return strings
to the caller, containing the tag or tags that will create the requested
form element. You are responsible for actually printing out these strings.
It's set up this way so that you can place formatting tags
around the form elements.
I The default values that you specify for the forms are only
used the B time the script is invoked (when there is no query
string). On subsequent invocations of the script (when there is a query
string), the former values are used even if they are blank.
If you want to change the value of a field from its previous value, you have two
choices:
(1) call the param() method to set it.
(2) use the -override (alias -force) parameter (a new feature in version 2.15).
This forces the default value to be used, regardless of the previous value:
print textfield(-name=>'field_name',
-default=>'starting value',
-override=>1,
-size=>50,
-maxlength=>80);
I By default, the text and labels of form elements are
escaped according to HTML rules. This means that you can safely use
"" as the label for a button. However, it also interferes with
your ability to incorporate special HTML character sequences, such as Á,
into your fields. If you wish to turn off automatic escaping, call the
autoEscape() method with a false value immediately after creating the CGI object:
$q = CGI->new;
$q->autoEscape(0);
Note that autoEscape() is exclusively used to effect the behavior of how some
CGI.pm HTML generation functions handle escaping. Calling escapeHTML()
explicitly will always escape the HTML.
I Some of the form-element generating methods return
multiple tags. In a scalar context, the tags will be concatenated
together with spaces, or whatever is the current value of the $"
global. In a list context, the methods will return a list of
elements, allowing you to modify them if you wish. Usually you will
not notice this behavior, but beware of this:
printf("%s\n",end_form())
end_form() produces several tags, and only the first of them will be
printed because the format only expects one value.
=head2 Creating an isindex tag
print isindex(-action=>$action);
-or-
print isindex($action);
Prints out an tag. Not very exciting. The parameter
-action specifies the URL of the script to process the query. The
default is to process the query with the current script.
=head2 Starting and ending a form
print start_form(-method=>$method,
-action=>$action,
-enctype=>$encoding);
<... various form stuff ...>
print end_form;
-or-
print start_form($method,$action,$encoding);
<... various form stuff ...>
print end_form;
start_form() will return a
tag.
start_form()'s enctype argument tells the browser how to package the various
fields of the form before sending the form to the server. Two
values are possible:
=over 4
=item B
This is the older type of encoding. It is compatible with many CGI scripts and is
suitable for short fields containing text data. For your
convenience, CGI.pm stores the name of this encoding
type in B<&CGI::URL_ENCODED>.
=item B
This is the newer type of encoding.
It is suitable for forms that contain very large fields or that
are intended for transferring binary data. Most importantly,
it enables the "file upload" feature. For
your convenience, CGI.pm stores the name of this encoding type
in B<&CGI::MULTIPART>
Forms that use this type of encoding are not easily interpreted
by CGI scripts unless they use CGI.pm or another library designed
to handle them.
If XHTML is activated (the default), then forms will be automatically
created using this type of encoding.
=back
The start_form() method uses the older form of encoding by
default unless XHTML is requested. If you want to use the
newer form of encoding by default, you can call
B instead of B. The
method B is an alias to B.
JAVASCRIPTING: The B<-name> and B<-onSubmit> parameters are provided
for use with JavaScript. The -name parameter gives the
form a name so that it can be identified and manipulated by
JavaScript functions. -onSubmit should point to a JavaScript
function that will be executed just before the form is submitted to your
server. You can use this opportunity to check the contents of the form
for consistency and completeness. If you find something wrong, you
can put up an alert box or maybe fix things up yourself. You can
abort the submission by returning false from this function.
Usually the bulk of JavaScript functions are defined in a