gnewcs has asked for the wisdom of the Perl Monks concerning the following question:

I've been looking around for answers for a while but I think it's time to put this to the experts. I'm about spent and need some direction!

I have been trying to create XHTML 1.1 Strict using cgi.pm, and trying to bend it to make it work. I'm starting to question whether I'm heading in the right direction though. For example, to get a header that validates, I have to hack the output (the sub is listed at the end of this post).

The manual says to generate meta http-equiv tags this way, but the output is:

<meta http-equiv="content-language" content="en-gb">

But to validate, we need:

<meta http-equiv="content-language" content="en-gb" />

Also, the script produces a language attribute which is invalid:

<script language="JavaScript" src="/sort.js" type="text/javascript"></script>

I also removed the <?xml... > line to prevent windows quirk mode. There are also other problems such as:

scrolling_list(-name=>'source',-values=>{%list}, -size=>5, -multiple=>'true')

which produces:

<select name="source" size="5" multiple>

but we need:

<select name="source" size="5" multiple="multiple">

to validate. The same is true for selected and checked.

As you can see below, I got around this using s//. But surely there must be a better way?

Is it still the case that the CGI module is not xhtml standards complient or it's still buggy? Are there any better solutions?

Here's my sub:

sub getheader { my $query = new CGI; my $hdr = $query->header(); $_ = $query->start_html( -title=>$_[0], -meta=>{ 'description'=>'Test', 'keywords'=>'test', 'rating'=>'general', 'robots'=>'all' }, -head=>[ meta({ -http_equiv=>'content-language', -content=>'en-gb@' }), meta({ -http_equiv=>'content-type', -content=>'text/html; charset=iso-8859-1@' })], -style=>{ -src=>'/atlantia.css', -media=>'screen', -charset=>'utf-8' }, -dtd=>'-//W3C//DTD XHTML 1.1 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd', -script=>{ -src=>'/sort.js' }, -onLoad=> defined($_[1]) ? "document.$_[1].focus()" : '', ); #----remove xml prologue <?xml version...> from before doctype to prevent windows 'quirk mode' s/\n//; $_ = $'; #----remove invalid language tag s/language=\"JavaScript\"//; #----replace invalid tag end > with /> for meta http-equiv s/@\">\n/\" \/>\n/g; return $hdr.$_; }

I am running perl v5.6.1 under 5.2.1-RELEASE FreeBSD i386, with cgi version 2.752

Replies are listed 'Best First'.
Re: CGI and XHTML
by tachyon (Chancellor) on Apr 13, 2004 at 16:12 UTC

    2.752 is very old. In fact it dates from 2/3/2001. Why not try a current version before you .....

    $CGI::VERSION = 3.00 <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U +S"><head><title>Untitled Document</title> <meta name="description" content="Test" /> <meta name="robots" content="all" /> <meta name="rating" content="general" /> <meta name="keywords" content="test" /> <meta http-equiv="content-language" content="en-gb@" /> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1 +@" /> <link rel="stylesheet" type="text/css" href="/atlantia.css" foo="bar" +media="screen"/> <script src="/sort.js" type="text/javascript"></script> </head><body onload="document..focus()"> <h1>Try a current version</h1> <select name="source" size="5" multiple="multiple"> </select>

    cheers

    tachyon

Re: CGI and XHTML
by BUU (Prior) on Apr 13, 2004 at 21:26 UTC
    Better idea: Stop using CGI.pm to generate html. Either here documents or better yet, templates would be vastly better.

      Well I used to use here documents as I think most people probably do, but after seeing so many articles of the 'you should be using cgi, no really...' kind, I started using it to produce the html too.

      In fact, I've grown to like the syntax, and I love how long winded checkbox groups, select boxes and the like can be created with one line and a hash. It really seems to reduce the code involved, and make the process a lot quicker, but I guess if one isn't used to the syntax, it can be quite hard to read.

      I'm not sure about templates. Surely you still have to write the code for all different types of code, which could be done with here docs anyway?

      PS. I now have cgi 3.04. Never thought to check that, I just assumed I had the latest port for some reason. Seems to work well.

        The real point of templates, and to a much lesser extent heredocs, isn't to reduce the work involved in creating the html in the first place (although it can). The real point is to seperate code and presentation. The html is the presentation, the perl is the code. Seperating them has many benefits, most of which have been listed elsewhere before.