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

Hi Monkees,
The following code produces a small error - a "/>" at the end of each img tag. I've added an example at the end of the script. What am I doing wrong?
use strict; use CGI qw/:all/; my $thumbs_url='./pics/thumbs/'; my $images_url='./pics/'; my @files; my $file; # main prog get_list(); print_thumbs(); exit; # subroutines sub get_list{ @files = glob("../pics/*.jpg"); } sub print_thumbs{ for(my $n = 0; $n < @files; $n++){ $files[$n] =~ /.*\/(.*)(\..*)/; $file = $1 . $2; print h3($1) . "\n"; print a( {-href => "$images_url" . $file}, img({-src => "$thumbs_url" . $file, -alt => $file, -border => "0 +"}) ); print "\n" . p($1) . "\n"; } }
The error looks like this:
<h3>sand1</h3> <a href="./pics/sand1.jpg"><img alt="sand1.jpg" border="0" src="./pics +/thumbs/sand1.jpg" /></a> <p>sand1</p> (note the /> at the end of the img tag)
Tom Melly, tom@tomandlu.co.uk

Replies are listed 'Best First'.
Re: CGI IMG tag error
by bikeNomad (Priest) on Jun 08, 2001 at 19:14 UTC
    Recent versions of CGI.pm default to producing XHTML output. Non-bracketing tags like your <img tag end with />. This is compatible with typical HTML browsers because of the space preceding it.

    Use the -no_xhtml pragma to inhibit this:

    use CGI '-no_xhtml';
      <rant>
      I've always strongly disliked (hated?) CGI.pm because of its HTML producing bloat. Now that's doing XHTML nonsense (goddammit, the fact typical browser authors can't parse their way out of a wet paper bag isn't a reason to violate standards - XHTML is not, and will never be, HTML) I've one more reason to burry CGI.pm under several cubic miles of concrete.

      CGI.pm must die.
      </rant>

      -- Abigail

(boo)Re: CGI IMG tag error
by boo_radley (Parson) on Jun 08, 2001 at 19:14 UTC
    That's not an error, that's part of the XHTML standard.
    Whenever a tag doesn't have an explicit closing tag, like  <a href="blah"> something</a>, XHTML dictates that it should have a / at the end of the tag. Since IMG stands alone, it receives the slash.
    You should also see this with things like the br tag and the p tag. for more info, go check out http://www.w3c.org or read o'reilly's "HTML and XHTML : the definitive guide".

    update - 1 (one) kudo for briac

    . update - 1 more kudo for ouefmayo.

      The nitpick instant™

      boo, you should know that XHTML dictates lowercased tags and attributes, and attributes values needs to be quoted!

      your example above should look like:

      <a href="blah">something</a>

      - briac!

      Note to self - learn basics of xhtml. I've even started reading an OR book about xhtml, but just didn't twig. Doh!

      BTW boo_radly shouldn't you always explicitly close the P tag? ;)

      Thanks Monkees

      Tom Melly, tom@tomandlu.co.uk

        In HTML, closing P tags is often unnecessary (since, let's face it, as things stand HTML is about presentation as well as content). A subsequent opening P tag implies that the previous paragraph has ended. In a fairly smallish SGML application with a fixed set of tags with more-or-less well defined meanings such as HTML, you can get away with such things. But in XML, where golly knows ( and golly != the XML parser ) what each tag is supposed to mean, you can't take such liberties. So even though XHTML is a descendant of HTML, since it is an application of XML, it's ipso facto gotta conform to the XML standard.

        Weasel words courtesy [ favourite local politician ]

        perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: CGI IMG tag error
by marcink (Monk) on Jun 08, 2001 at 19:16 UTC
    There is nothing wrong with that -- /> is the XMLish way of ending tags that have no corresponding closing tags.

    -mk
Re: CGI IMG tag error
by Anonymous Monk on Jun 09, 2001 at 05:27 UTC
    xhtml standards specify tags must be closed, so tags such as img, br, and hr are supposed to end that way:
    sand1<br/> <img alt="sand1.jpg" src="./pics /thumbs/sand1.jpg" /> <hr />
    This doesn't cause any problems with older browsers, so there's no need to change the code that produces the html.