When you call any of the methods in Table with no argument,
they generate only the corresponding tag. If you call any of the methods with
a single scalar argument, they will generate both the appropriate start and
end tags around that argument; with multiple scalar arguments it concatenates
the arguments and then surrounds them with the appropriate tags.
Perhaps the most interesting feature of the helper methods is that when they are passed a reference to an array (or an anonymous array), the will distribute the start and end tags over each element of the array. This feature is especially useful for lists and tables.
Consider producing HTML for a table containing the names of animals. Using the distributive feature of the helper methods, we could write:
print $q->ul($q->li(['dog','cat','chicken','duck','goose']));which will generate the following HTML code, reformatted for easier reading.
<UL><LI>dog</LI> <LI>cat</LI> <LI>chicken</LI> <LI>duck</LI> <LI>goose</LI></UL>
The distributive feature also makes producing HTML tables simple. Suppose we have a file containing names, room numbers and email addresses for employees, separated by tabs, and we wish to generate a table of those values. The helper methods could be used as follows:
while(<FILE>){ @info = split("\t"); push @rows, $q->td(\@info); } print $q->table($q->Tr(\@rows));
Finally, if the first argument to the helper methods is a reference to a hash (or an anonymous hash), the named elements are used as arguments to the tag in question. Furthermore, if the technique of the previous section using array references is followed, those arguments are further distributed over the tags. So to center the text in all of the cells of the previous table, we could modify the call to the td method as follows:
push @rows, $q->td({align=>'center'},\@info);