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

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

Replies are listed 'Best First'.
Re^5: XML tags using perl CGI
by Joost (Canon) on Feb 06, 2007 at 13:09 UTC
    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.

        Although I do wish there was a simple module to decode query parameters that isn't part of a framework

        No need to wish, since it already exists: CGI::Simple -- it gives an interface to the CGI parameters that would be familiar to ex-users of CGI.pm without any of the hacky HTML generation cruft.

        Although I do wish there was a simple module to decode query parameters that isn't part of a framework

        URI?

Re^5: XML tags using perl CGI
by davorg (Chancellor) on Feb 06, 2007 at 13:15 UTC
    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?
        What does the qw do

        qw// is the "quote words" operator. It's a quick way to build a list from whitespace separated string. It's documented in the section on Quote and Quote-like Operators in perlop. Like all of the quote-like operators you can use any delimiter that makes sense for you, so qw/:standard :html2/ is exactly the same as qw(:standard :html2).

        what is the difference between a : parameter, like :standard, and a - parameter, like -no_xhtml?

        There are no fixed rules for how these parameters work - only conventions. One convention is that a module should use Exporter to control its exports (the module's exports are your programs imports). Exporter defines the ':foo' syntax for groups of symbols that are exported together. CGI.pm doesn't use Exporter to manage its exports, but it does stick to the ':foo' syntax to designate export groups. The '-foo' syntax for other parameters is (as far as I know) something that was invented by the author of CGI.pm, but these parameters are "options" that control how CGI.pm works for the '-foo' syntax was almost certainly chosen to mirror the common syntax of options to command line options. It also mirrors the syntax used to pass parameters to many CGI functions (e.g. start_html(-title => 'foo')).