Masatoshi Mizuno
and 1 contributors



  - CGI Form for using by HTML::Template is generated.
  - HTML::Template::Associate FormField plugin.


 use CGI;
 use HTML::Template;
 use HTML::Template::Associate::FormField;

 ## The form field setup. ( like )
 my %formfields= (
  StartForm=> { type=> 'opt_form' },
  Name  => { type=> 'textfield', size=> 30, maxlength=> 100 },
  Email => { type=> 'textfield', size=> 50, maxlength=> 200 },
  Sex   => { type=> 'select', values=> [0, 1, 2],
             labels=> { 0=> 'please select !!', 1=> 'man', 2=> 'gal' } },
  ID    => { type=> 'textfield', size=> 15, maxlength=> 15 },
  Passwd=> { type=> 'password', size=> 15, maxlength=> 15,
             default=> "", override=> 1 },
  submit=> { type=> 'submit', value=> ' Please push !! ' },

 ## The template.
 my $example_template= <<END_OF_TEMPLATE;
 <head><title>Exsample template</title></head>
 <h1>Exsample CGI Form</h1>
 <form <tmpl_var name="__StartForm__">>
 <tr><td>Name     </td><td> <tmpl_var name="__NAME__">   </td></tr>
 <tr><td>E-mail   </td><td> <tmpl_var name="__EMAIL__">  </td></tr>
 <tr><td>Sex      </td><td> <tmpl_var name="__SEX__">    </td></tr>
 <tr><td>ID       </td><td> <tmpl_var name="__ID__">     </td></tr>
 <tr><td>PASSWORD </td><td> <tmpl_var name="__PASSWD__"> </td></tr>
 <tmpl_var name="__SUBMIT__">

 ## The code.
 my $cgi = CGI->new;
 # Give CGI object and definition of field EEE
 my $form= HTML::Template::Associate::FormField->new($cgi, \%formfields);
 # Give ... ::Form Field object to associate 
 my $tp  = HTML::Template->new(
            scalarref=> \$example_template,
            associate=> [$form],
 # And output your screen
 print $cgi->header, $tp->output;

   or, a way to use not give associateEEE

 my $cgi = CGI->new;
 my $form= HTML::Template::Associate::FormField->new($cgi, \%formfields);
 my $tp  = HTML::Template->new(scalarref=> \$example_template);
 # set up the parameter directly
 $tp->param('__StartForm__', $form->param('StartForm'));
 $tp->param('__NAME__',   $form->param('Name'));
 $tp->param('__EMAIL__',  $form->param('Email'));
 $tp->param('__SEX__',    $form->param('Sex'));
 $tp->param('__ID__',     $form->param('ID'));
 $tp->param('__PASSWD__', $form->param('Passwd'));
 $tp->param('__SUBMIT__', $form->param('submit'));

 print $cgi->header, $tp->output;

 # If you move it as a plug-in of HTML::Template::Associate.
 # * The code is an offer from "Alex Pavlovic" who is the author of HTML::Template::Associate.

 use HTML::Template;
 use HTML::Template::Associate;

 my $associate = HTML::Template::Associate->new ({
    target => 'FormField',
    cgi    => $cgi,
    form_fields => \%formfields

 my $template= HTML::Template->new (
   scalarref=> \$example_template,
   associate=> [ $associate ],

 print $cgi->header, $template->output;


This is Form Field object using bridge associate option of HTML::Template. Fill in the Form Field which made from object follow the template. If the Form Field data which was input at the previous screen exist, it is easy to make code, because process (CGI pm dependense) of fill in Form is automatic.

Form Field Setup

  • The Form of the definition data of Form Field is HASH. And, contents of each key is HASH, too.

  • The name of each key is hadled as name of Form Field. Also, in case of hadling by HTML::Template, the name of key become enclosed with '__' . For example, Field that was defined Foo correspomds to __FOO__ of template.

  • The contents of each key certainly be defined the key ,type, which shows type of Form Field.

  • The value of designate to type is same as method for making Form Field of Please refer to document of for details.

    startform , start_multipart_form , endform , textfield , filefield , password_field , textarea , checkbox , radio_group , popup_menu , optgroup , scrolling_list , image_button , defaults , button , reset

  • And others, be possible to designate for extension Field type at HTML::Template::Associate::FormField are as follows:

    form ... other name of startform. (%)

    start_upload_form ... other name of start_multipart_form. (%)

    upload_form ... other name of start_multipart_form. (%)

    opt_form ... return only a part of attribute of startform.

    opt_multipart_form ... return only a part of attribute of start_multipart_form.

    opt_upload_form ... other name of opt_multipart_form.

    hidden_field ... return all of no indication Field which is seting up.

    hidden ... other name of hidden_field

    text ... other name of textfield.

    file ... other name of filefield.

    password ... other name of password_field.

    radio ... other name of radio_group.

    select ... other name of popup_menu.

    image ... other name of image_button.

    (%) In case of no indication Field was set up , connect the no indication Field and return the value.

  • In case of you'd like to acquire the name from CGI query - it is different name of the key which definition of Form Field, designate for the name of CGI query as alias to contents of each key.

     $cgi->param('Baz', 'Hello!!');
     my %formfields= ( 'Foo'=> { alias=> 'Baz', type=> 'textfield' } );




  1. Accept CGI object or HASH reference to the first parameter.

  2. Accept definition of CGI Form (HASH reference) to the second parameter.

    $form= HTML::Template::Associate::FormField->new($cgi, \%formfields);


Constructor for HTML::Template::Associate.

param, params

Set up or refer to definition parameter of CGI Form.

  • Get all keys which is defined as Form Field.

    (All keys which was able to get by this are enclosed by '__')


  • Get the Form Field which was designated.





Access to object which control no indication Field.

  • Add to no indication Field.

    $form->hidden->set('Foo', 'Hoge');

  • Get all no indication Fields which was set beforehand.


  • Get no indication Field which was designated.


  • Erase the data of no indication field which was designated.


  • Find out the no indication Field was set or not.

    $form->hidden->exists ? 'true': 'false';

  • Erase all of no indication Field which was set.



Export no indication Field, object.

  • Get no indication field, object.

    my %hash = ( 'Foo'=> 'Form Field !!' );

    $hidden = $form->hidden_out(\%hash);

  • Usable methods are same as hidden.

    $hidden->set('Baz', 'Hoge');



  • Hidden object which was exported is not linked with startform and, start_multipart_form. No indication field which was formed at this object is please give to param method of HTML::Template.

    $tp= HTML::Template->new( ..... );

    $tp->param('HIDDEN_FIELD', $hidden->get);


In case of errors in the definition of Form field, return this error message instead of Form field.

  • Can't find field type.

    There is no designation of type in definition Form field.

  • Can't call "%s" a field type.

    Errors in definition form of type.


When you call a function start_form without an action attribute by old CGI module, you might find a caution "Use of uninitialized value". In this case, let's upgrade to the latest CGI module.


 HTML::Template, CGI


Generously contributed to English translation by:

Ayumi Ohno

Special Thanks!


Masatoshi Mizuno <>


Copyright (C) 2004-2007 by Bee Flag, Corp. <>, All Rights Reserved.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 313:

Non-ASCII character seen before =encoding in 'EEE'. Assuming ISO8859-1