Reputation: 21
I needed to do some simple HTML and was having some trouble getting CGI.pm to output HTML5 instead of strictly XHTML 1.0 or HTML 4.01. I looked at the other stackoverflow post on the topic. I neither had the time or desire to learn a templating framework just to complete this task. I totally know that the CGI.pm README says that HTML generation functions should no longer be used, and yet modern browsers largely still support what they generate, so why not? What did I do?
As Dan Hale had alluded, I 'solved' this problem using brute force. A complete script example testing HTML5 mixed with CGI.pm html generation functions is below.
#!/usr/bin/perl
use CGI ':all';
use CGI::Carp 'fatalsToBrowser', 'carpout';
# $html5 is accumulator for HTML string
my $html5;
# <html> tag and <head> section
my $dtd = '<!DOCTYPE html>'; # HTML5 DTD
my $intro = '<html lang="en">';
my $title = "This is a test of HTML5 compliant headers using CGI.pm";
my $css3 = "/screen.css";
$html5 .= start_html(
-title => $title,
-style => {
'src' => "$css3",
}
);
# KLUDGE: CGI.pm doesn't support HTML5 DTD or the xmlns in the <html> tag; replace the ones it uses
$html5 =~ s{<!DOCTYPE.*?>}{$dtd}s;
$html5 =~ s{<html.*?>}{$intro}s;
print header(),
"$html5",
"This is a test of the HTML5 required textfield entry.",
start_form(-action=>'/cgi-bin/processform.pl'),
p(),
"Name: <input type='text' name='name' required>",
p(),
submit(-name=>'submit',-value=>'Submit',-id=>'submit'),
p(),
end_form(),
end_html();
exit(0);
Upvotes: 1
Views: 96
Reputation: 132896
The docs tell you not to use the HTML generating functions because it's generally painful to maintain HTML that is written as code. It's much easier to use some sort of templating thing that fills in the bits you need.
Once you start doing the sorts of things you are doing, you've gone past the point where you should still be using the original library.
The quick win could be that you stop using CGI.pm's start_html
and simply define your own. Instead of taking the output of a function that doesn't do what you want, make your own to return exactly what you want. But once you do that, you're pretty close to templating.
That template could be a string in the program, or it could be a separate file. You could use something simple, such as Text::Template or go full on with something like Mojo::Template. There are many others. Now you won't have to do the substitutions and various other code things. And, many of the template thingys allow you to include other templates, such as your own start_html
template.
You might start by taking the current HTML you output and make that the template.
CGI.pm has another problem. It's ideas of what you should but in headers and the HTML is based on what we were doing in the 90s, as in thirty years ago. It's much easier to adapt to that through a template where you a simply type out (copy and paste? delete?) the new hotness.
I'd much rather look at a template than have to suss out what start_html
does and why a substitution might be broken based off stuff I can't easily look at.
Upvotes: 1