http://www.perlmonks.org?node_id=539351

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

Now this may appear to be a weird question, but back when I started learning Perl in 2000 I found myself using it primarily for CGI scripts.

At first, I did use OOP CGI until I quickly realized it was a major pain in the ( | ). But I always

use CGI qw/:standard/; print header, start_html();
And it just hit me: is there a reason WHY I am doing this? I process forms all the time just by calling params and url_params, which I doubt come from CGI (or do they?). If not, can you see any specific reason to use CGI.pm instead of typing out the headers manually?

Sure, it may save a few lines of code for the header, but if it's not necessary it would be nice to get rid of the dead weight.

Let me know what your thoughts are and whether or not there's actually a reason to use CGI.pm if you're not using it with objects.

Replies are listed 'Best First'.
Re: to use or not to use CGI.pm
by McDarren (Abbot) on Mar 27, 2006 at 02:40 UTC
Re: to use or not to use CGI.pm
by davorg (Chancellor) on Mar 27, 2006 at 08:43 UTC

    It's important to note that CGI.pm implements a good autoloading mechanism. This means that subroutines are only ever loaded when you use them and that therefore there is very little "dead weight".

    Having said that, it's a good idea to take a look at the export sets that CGI.pm provides and start using one that better models the set of subroutine that you use. Personally I always use TT to generate any HTML, so I only every use the CGI.pm subroutines that give access to CGI protocol functionality. For that reason, most of my CGI programs start with:

    use CGI ':cgi';

    This means that I can't accidently start using any of the HTML generation functions, thereby breaking the separation of processing and display logic in my program.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: to use or not to use CGI.pm
by ChemBoy (Priest) on Mar 27, 2006 at 03:34 UTC

    I process forms all the time just by calling params and url_params, which I doubt come from CGI (or do they?).

    Yes, they do. :-)

    So yes, you should continue to use CGI.pm, until such time as you have an active (and demonstrable) need not to.



    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders

Re: to use or not to use CGI.pm
by dorward (Curate) on Mar 27, 2006 at 07:06 UTC

    There seem to be three questions here

    Should I use CGI.pm for generating markup?

    This comes down to largely personal preference. I dislike it and tend towards Template-Toolkit instead. On the other hand, it does automatically handle setting of the value attribute to match submitted data (which may or may not be something you want).

    Does CGI.pm export the param() subroutine?

    Yes, it does

    Should I use CGI.pm for processing form parameters?

    You should use something. If CGI.pm is too heavy for you, perhaps you might consider CGI::Lite (I've never used it, but haven't heard anything bad about it.). You might also consider using Apache::Request under mod_perl.

      Yeah, I'd been sorta looking into Template-Toolkit, too. Seems like that it would be a better fit if you want to read in an HTML template and fill in certain values. My thought is that if you use that, then people maintaining a website using Template-Toolkit wouldn't need to know the CGI.pm syntax or even perl; they would pretty much just have to know perl, and with so many HTML GUI editors like Dreamweaver, BBedit and Microslosh Word, this could make sense. That way, you can hand off more of the mundane day-to-day stuff to someone else, and spend your time reading slashdot or perlmonks. :)

      I've also heard a lot about HTML::Mason as well. That's if you have a large site to manage. I saw a job posting for ebay a while ago, and here are some other sites that use it. I think there are some Oreilly books on them, too.

      -- Burvil

Re: to use or not to use CGI.pm
by zer (Deacon) on Mar 27, 2006 at 07:37 UTC
    CGI.pm makes it easy to accept both 'POST' and 'GET' types of data. If you do that manualy your head can be hurting a bit. Its also easier than learning the http protocal, so it saves time.