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

Greetings Monks,
I've just adopted a perl CMS abandoned on SF. While it's a bit "legacy", it's fairly advanced, in spite of it - think AMC Gremlin. :)
Anyway, since I've found my way back to perl from PHP, I'm finding situations I hadn't had to deal with in Perl, that I can't seem to figure a good solution for. In an attempt to generate well-formed application/xhtml+xml documents, but still not cause legacy browsers to prompt to download the document, I used the following in PHP
<?php header("Vary: Accept"); if (stristr($_SERVER["HTTP_ACCEPT"], "application/xhtml+xml")) header("Content-Type: application/xhtml+xml; charset=utf-8"); else header("Content-Type: text/html; charset=utf-8");
This way, modern UA's recieved the document as intended, and legacy UA's got "tag soup".
For the life of me, I can not figure the equivalent for perl. Would any of you kind, brilliant Monks be willing to point me in the right direction. : ))

Thank you very much for all your time and consideration.
Apologies if it seems I rambled.
--
use qw(:perl:always);
use qw(:perlmonks:daily);

Replies are listed 'Best First'.
Re: well formed XHTML != tag soup
by Anonymous Monk on Jan 07, 2010 at 09:23 UTC
    This might be one way
    use CGI; if( CGI->http('accept') =~ m'\Qapplication/xhtml+xml\E'i ){ print CGI->header( 'application/xhtml+xml; charset=utf-8 '); } else { print CGI->header( 'text/html; charset=utf-8 '); }
    CGI, CGI Programming
      Greetings "Anonymous Monk", and thank you for your reply.
      Really, you make it look so simple. :) However, I'm still a bit concerned. If I'm not mistaken, I need the "Vary" handle - this is "UA speak", (robots simply ignore it - as do those poking about your web server, looking to do bad things) and gives me something to "grab" onto (Vary handle). Unless I'm mistaken, w/o it, there's nothing to return. The conditional (as you wrote it) is otherwise "spot on".

      Thank you again for taking the time to respond.

      --
      use qw(:perl:always);
      use qw(:perlmonks:daily);

        Then output the Vary: header like you output the Content-Type header.

        need the "Vary" handle

        clue bat + manual = beat it :)

        Sorry, couldn't resist after "handle".

        CGI->header ( -content_type => 'stuff', -vary => 'Accept', -nph => 1, );