|
NAMEText::Bind - Bind Perl structures to text files SYNOPSIS use Text::Bind;
# Create a new object
$text = new Text::Bind; # or
$text = new Text::Bind "page.html" # or
$text = new Text::Bind \*IN;
# Bind a string value to a data site
$text->bind_site("astring", "Hello World!");
# Bind a function to a data site
$text->bind_site("form", \&generate_form);
# Bind a filehandle to a data site
$text->bind_site("filehandle", \*FILE);
# Bind an object to a data site
$some_object = new SomeClass;
$text->bind_site("object", $some_object);
# Read text
$text->read_text(\*OUT, "page.html"); # or
$text->read_text(\*OUT, \*IN); # or
$text->read_text(\*OUT); # or
$text->read_text;
DESCRIPTIONText::Bind allows you to bind Perl structures (strings, routines, filehandles, objects) to specific locations (called data sites) in text files. The main purpose of this module is to support HTML templates for CGI programs. Therefore, HTML pages design can be kept separate from CGI code. However, the class is general enough to be used in other contexts than CGI application development. For example, it could be used to do form letters. To create a new object, do one of the following: $text = new Text::Bind;
$text = new Text::Bind $filename;
$text = new Text::Bind \*FILE;
If no argument is given during object instantiation, then the input must be specified during the read_text method. Otherwise, a filename or a filehandle can be listed to specify the input source of the text data. To have the data processed, use the read_text method in one of the following ways: $text->read_text;
$text->read_text(\*OUT);
$text->read_text(\*OUT, $filename);
$text->read_text(\*OUT, \*FILE);
When called with no arguments, input is read from what is specified during object instantiation, and output goes to STDOUT. If arguments are specified, the first argument is the output filehandle. If undefined, STDOUT is used. The second argument is the filename or the filehandle of the input. If not defined, input is read from what is specified during object instantiation. The syntax for specifying data sites in the input and how to bind Perl structures to those sites is covered in the following sections. Data Site SyntaxTo define a data site, the syntax is as follows: ##PL_name=value## where the components mean the following:
The name is case insensitive.
Data sites that do not have a binding during processing of text input are expanded to the empty string. Duplicate sites can occur. The binding will be reexecuted each time the site occurs. Example data sitesA data site for an HTML form: <html>
<body>
##PL_site=inputform##
</body>
</html>
The call to bind_site may look like: $text->bind_site("inputform", \&create_form)
The following shows how a file can bound to a site: <html>
<body>
...
<hr>
##PL_file=copyright.html##
</body>
</html>
The contents of "copyright.html" will replace the site definition. The following shows how the output of a program can be included: ##PL_file=/bin/ls -l |## Data Site Bindings via bind_site MethodThe bind_site method takes 2 arguments, the name of the site to bind to and a value to define the value of the site during the read_text method. Example: $text->bind_site($name, $bind); The $name of the data site corresponds to the value of a ##PL_site=value## data site. The bind value can be one of the following Perl data structures:
Data Site LoopsOccasionally, there is a need to have a block of text be repeatable. For example, lets take the following text data: ##PL_beginloop##Item = ##PL_site=listvalue##
##PL_endloop##
And the following bindings: $text->bind_site('listvalue', [ 1, 2, 3, 4 ]);
Will generated the following output: Item = 1
Item = 2
Item = 3
Item = 4
The text between the begin and end data sites is repeated until the evaluation of ALL data sites within the loop generate no more data. Within a loop, types of data sites are treated as follows:
Loop ExamplesA good example of loops is when populating an HTML table with data: <table>
##PL_beginloop##
<tr>
<td>##PL_site=lastname##</td>
<td>##PL_site=firstname##</td>
<td>##PL_site=street##</td>
<td>##PL_site=city##</td>
<td>##PL_site=state##</td>
<td>##PL_site=zip##</td>
</tr>
##PL_endloop##
LIMITATIONS
AUTHOREarl Hood, earlhood@bigfoot.com http://www.oac.uci.edu/indiv/ehood
|