I was tracking down missing HTML4 LABEL tags (semantic tags that improve usability by associating mouse clicks for text labels with form inputs) in CGI.pm scripts using the radio_group() function, and discovered something that merits a blog post in case other programmers run into the same problem …
Using the -no_xhtml directive to CGI.pm disables the LABEL tag, regardless of what EXPORT_TAGS you specify. So even if :standard, :all, or :html4 are specified, the LABEL tag is not emitted.
The :html4 EXPORT_TAG looks like this:
':html4'=>[qw/abbr acronym bdo col colgroup del fieldset iframe
ins label legend noframes noscript object optgroup Q
thead tbody tfoot/],
But the plot thickened as I continued testing, because even if you don’t use -no_xhtml, the following incorrect behavior occurs …
CGI.pm has several EXPORT_TAGS, such as :html2, :html3 and :html4 that should restrict the available HTML tags according to the HTML version you want. However, LABEL, which appeared in HTML4 and is correctly defined only in the :html4 tag, is always displayed around radio_groups, etc.
Then I noticed CGI.pm conditional code like this:
return $XHTML ? CGI::label(qq{<input type="checkbox" name="$name" value="$value"
$tabindex$checked$other/>$the_label})
: qq{<input type="checkbox" name="$name" value="$value"
$checked$other>$the_label};
and
if ($XHTML) {
push @elements,
CGI::label(
qq(<input type="$box_type" name="$name" value="$_"
$checkit$other$tab$attribs$disable/>$label)).${break};
} else {
push(@elements,qq/<input type="$box_type" name="$name" value="$_"
$checkit$other$tab$attribs$disable>${label}${break}/);
}
I don’t see why XHTML mode is needed for a HTML4 tag like LABEL. And if :html2 or :html3 is specified, LABEL should not be emitted.
At this point, I would have to question all of the XHTML-related code that was added to CGI.pm since 2.67. See changelog.
Another bug is that there should be a space before $checked above, so that the literal ‘checked’ becomes ‘ checked’.
Test script:
use CGI qw / -no_xhtml :html4 :form /;
#use CGI qw / :html4 :form /;
print 'Perl version: ', $], ', ', '$CGI::VERSION: ', $CGI::VERSION, "\n";
print radio_group(
-name=>'how far',
-values=>['10 ft','1 mile','10 miles','real far'],
-default=>'1 mile',
);
print "\n",
bdo("HTML4 BDO tag"),
"\n";
The output incorrectly omits LABEL tags.
Tested with Perl 5.8.5 (CGI.pm 3.29) and 5.8.8.


