in reply to Re^2: XML tags using perl CGI
in thread XML tags using perl CGI

the standard also says if I set :html2, xhtml will go away (but it doesn't seem to have an effect)

Not sure which "standard" you're talking about there.

The ':html2' import tag just defines the set of functions that are imported into your symbol table. As you're using the Object interface to CGI.pm it has no effect at all on your program.

my $query = CGI->new; print $query->start_html(-title=>'Argh!', -background=>"../images/$SiteName.background.jpg", -link=>'brown', -vlink=>'#8b4513', -no_xhtml=>1, #this has no effect... -dtd=>'3.2 DTD' # this does the trick, but not listed in # the standard as to how to set it -- I # hope this is right... }

Ah, you seem to be using "standard" to mean "the CGI.pm documentation", which is slightly unusual :-)

The docs say:

If start_html()'s -dtd parameter specifies an HTML 2.0 or 3.2 DTD, XHTML will automatically be disabled without needing to use this pragma.

I think that means that you can override the whole DTD with this parameter. You can't just give it a string like "3.2 DTD", you need to give it a complete valid HTML DTD.

Also, you're using the -no_xhtml pragma wrong (which is why it has no effect). It's not a parameter to start_html() (and nowhere does the documentation imply that). It's in the list of pragmas - which are arguments you give in the "use CGI" statement.

I think that what you want is something like this:

use CGI '-no_xhtml'; my $c = CGI->new; print $c->start_html;

Which produces:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en-US"><head><title>Untitled Document</title> </head> <body>

And if I can just make one further comment. Embedding HTML markup in your code using the CGI.pm methods is a really nasty way to go about it. Your life will be much easier if you separate your HTML from your code using some kind of templating system (I recommend the Template Toolkit, but other options are available).

Update: s/usual/unusual/ - thanks to Corion for pointing it out.

Replies are listed 'Best First'.
Re^4: XML tags using perl CGI
by pmcaveman (Novice) on Feb 06, 2007 at 12:35 UTC
    Ok, that is good to know. I am not completely understanding what a pragma is -- I though that is a precompiler option -- same here, so it forces some flags into the interpreter for that module?

    Also, I am unsure how to format the following options:
    use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard :html2/; use CGI '-no_xhtml';
    So, when I use CG_:Carp, do I also need to 'use CGI', or is it redundant? Do I need one line, or two? How should I combine the quotes, slashes and other parameters?

    I am almost scared to ask what all of that means -- I am sure there is an ocean of documentation which describes it that I don't have time to read...

    Thanks for the help.

    -Kevin
      As far as I know, you still need to use CGI if you use CGI::Carp - especially if you also want to pass paremters to the CGI module.

      As for how to combine the import parameters (that is, the list of arguments to "use CGI") you can just lump them all together:

      use CGI qw(:standard :html2 -no_xhtml);
      Note that you really should not use html2 - HTML 2 is very outdated. Just using the -no_xhtml option should be enough.

        you still need to use CGI if you use CGI::Carp

        There is no need to use CGI if you want to use CGI::Carp. They are separate modules...

        In fact
        There is no need to use CGI ever!

        Although I do wish there was a simple module to decode query parameters that isn't part of a framework as sometimes query parameters are needed when no framework is required.

      I am sure there is an ocean of documentation which describes it that I don't have time to read...

      But you do have time to post here asking us to read the documentation for you.

      CGI.pm and CGI::Carp are two completely separate modules. CGI.pm has functions that give you easy access to various data that you need in the CGI environment and also contains "HTML shortcut" functions that are supposed to make it easier to construct HTML pages. CGI::Carp contains functions which improve the error messages that you get from CGI programs. It is completely possible to use one of these modules without the other. If you are using both of these modules then you need to include a "use" statement for both of them.

      When you use a module, you can give it various parameters as part of the "use" statement. These parameters can control various things. A common use of these parameters is to import various symbols into your program's main symbol table. If you see a parameter that begins with a ':' then it's the name of a set of symbols that are imported together. For example, in CGI.pm ':html2' imports all of the HTML 2.0 shortcut functions and ':cgi' imports all of the functions that deal with the CGI protocol. The list of these sets and what they contain is in the documentation.

      Notice two points in that documentation. Firstly the section is called "Using the Function-Oriented Interface". As these sets import sets of symbols into your symbol table, they are only useful if you are using CGI.pm's function-oriented interface. From all of the code you have shown us, it is obvious that you are using CGI.pm's OO interface. Therefore these tags are of no use to you at all and you shouldn't use them. Secondly, the definition of the ':standard' set says:

      Import "standard" features, 'html2', 'html3', 'html4', 'form' and 'cgi'.

      Therefore if you import the ':standard' set, it includes the ':html2' set and there is no reason to import the ':html2' set again.

      All of this means that the parameters on your line:

      use CGI qw/:standard :html2/;

      are a) overcomplex and b) completely unnecessary. You don't need them.

      You do, however, want the '-no_xhtml' pragma. So you need to have that in your use statement. It should look like this:

      use CGI qw/-no_xhtml/;

      It's worth noting that if you were using the function-oriented interface then you could mix both import tags and pragmas in the same statement like this:

      use CGI qw/-no_xhtml :standard/;

      All of this is in the documentation and although you think you're far too busy to read the documentation, I would urge you to make time to do so - as it will make you a better programmer.

        So, what is the difference bewteen :
        use CGI qw/:standard :html2/;
        and
        use CGI qw(:standard :html2);

        What does the qw do, and what is the difference between a : parameter, like :standard, and a - parameter, like -no_xhtml?

        Anyway, sorry for my ignorance, and the help/tutorial is greatly appreciated.

        BTW, for using this website, is it possible to see all of the items for a single node under one tree? What does the + and - do when reading through a node?