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

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

#!/fellow/monks.pl

The time has come for me to leave CGI's behind me, and start using mod_perl. It seems to have more benefits than CGI. As performance is my main driver, I'd be spending some time now on mod_perl.

Just a quick question on this. Would you still use cgi.pm, even though you'd be using mod_perl, and not CGI anymore?

And my final question: My scripts are full of the imfamous unweb procedure. I've managed to change that to use cgi.pm, for the obvious security issues. Is it important to use cgi.pm even for doing a simple thing like print "<h1>My site</h1>\n";? Personally, I'd prefer to keep using my print statements, unless there's some security or performance reason why I shouldn't.

Thanks!

#!/massyn.pl The early worm gets caught by the bird.

Replies are listed 'Best First'.
Re: use CGI or die, even with mod_perl?
by fruiture (Curate) on Apr 29, 2003 at 10:09 UTC

    The main problem solved by CGI.pm is correct evaluation of Query-String or STDINput and the correct output of HTTP Headers. I'd say HTML-generation is a nice feature for small scripts.

    As the input parameters and rules for HTTP Headers stay the same under mod_perl (depending on what handler you implement ...), it's still CGI.pm that will do these tasks correctly for you.

    As for the HTML stuff: You will agree that in a larger application you'll use templates anyway (i like the Template Toolkit). You only determine the HTML-output in small scripts and for them CGI.pm will save you typing and will produce correct HTML (better "will make it easy to produce correct HTML"). What you do is up to you.

    --
    http://fruiture.de
Re: use CGI or die, even with mod_perl?
by IlyaM (Parson) on Apr 29, 2003 at 13:39 UTC
    People use CGI module for two things:
    • For HTML generation. Some people might argue that you'd better use templating modules instead. Anyway it is not relevant to the question if you should use CGI with mod_perl - if you like CGI.pm functionality for HTML generation there is no "better" mod_perl replacement.
    • For query processing and file uploads processing. Here you can replace this module with Apache::Request. It has very similar API but in theory it should be faster and smaller as this module is writen in C (though I should note that it was never a bottleneck in my mod_perl projects).

    --
    Ilya Martynov, ilya@iponweb.net
    CTO IPonWEB (UK) Ltd
    Quality Perl Programming and Unix Support UK managed @ offshore prices - http://www.iponweb.net
    Personal website - http://martynov.org

Re: use CGI or die, even with mod_perl?
by hardburn (Abbot) on Apr 29, 2003 at 13:55 UTC

    Almost everyone agrees that anything fairly sizeable should use some kind of templating system. For small projects, there are three schools of thought:

    1. Good ol' print statements
    2. CGI.pm's HTML generation
    3. Go right to a templating system regardless of the size

    The print statement way is almost certainly the least maintainable, but maybe that won't be an issue for a small CGI. Using CGI.pm's HTML generation has a large following (not the least of which is merlyn). People of the other two schools often think that CGI.pm should only be about input processing and should ditch the HTML generation into a seperate module. However, there is probably too much code using the current system to change it now.

    I personally favor going right to a templating system (unless its a really quick-and-dirty job). Its probably the most maintainable, but will almost certainly take longer to get it done the first time through (depending on the complexity of the templating system you use).

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: use CGI or die, even with mod_perl?
by nite_man (Deacon) on Apr 29, 2003 at 09:05 UTC
    I use mod_perl and embed Perl code into html page using Embperl and Mason instead of CGI. Those tools are more powerfull, flexible and secure than CGI. But if you need you can run your CGI scripts under mod_perl: CGI to mod_perl Porting.
    Good luck!
          
    --------------------------------
    SV* sv_bless(SV* sv, HV* stash);
    
(jeffa) Re: use CGI or die, even with mod_perl?
by jeffa (Bishop) on Apr 29, 2003 at 14:46 UTC

    mod_perl++ :)

    Be sure and grab a copy of the mod_perl Developer's Cookbook if you have the cash. It gives examples of how to use CGI, Mason, HTML::Template, Template Toolkit, and Apache::ASP within mod_perl - plus a whole lot more. As a matter of fact, now that school is out i think it's time for me to read this a second time. ;)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: use CGI or die, even with mod_perl?
by tadman (Prior) on Apr 29, 2003 at 14:53 UTC
    To be honest, if you're making a mod_perl application you don't have to use the CGI module at all. Apache::Request does the handling for you, in a manner that is quite similar to CGI.

    There are a few tiny differences, but otherwise, you can survive without having to use CGI at all. Of course, you're using the mod_perl equivalent, so, it's not like you're playing with fire.
Re: use CGI or die, even with mod_perl?
by perrin (Chancellor) on Apr 29, 2003 at 16:24 UTC
    You don't need CGI.pn unless you want to make your scripts backwards-compatible to CGI. You don't need Apache::Request either, since you can just use the args() method of the Apache object you get passed. Here is a simple example Apache::Registry script:
    my $r = shift; %args = $r->args(); $r->send_http_header('text/html'); print "You said your name was " . $args{'name'};
Re: use CGI or die, even with mod_perl? - tad offtopic
by Beatnik (Parson) on Apr 29, 2003 at 17:12 UTC
    Not all that ontopic but not entirely off-topic either. While doing HTTP file uploading in mod_perl, I used Apache::Request and any form data besides the file field didn't get parsed properly. I followed the usual form construction and did some tests. What I discovered was that, on my system anyway, you can't use the plain $r->args or $r->content for parsing any other fields. I used Apache::Requests param, which is similar to CGI.pm to eventually access the other form field and everything went smooth from there. I've probably overlooked this in the POD or the eagle book somewhere. Just thought I'd share.

    Greetz
    Beatnik
    ... I'm belgian but I don't play one on TV.
Re: use CGI or die, even with mod_perl?
by Petras (Friar) on Apr 30, 2003 at 02:57 UTC
    I would visit Ovid's website and look at his CGI tutorial. He's a fanatic when it comes to security, and has even made someone as uncaring as me care a little. Of course, he's unintentionally planted some cracker ideas in my head, too ;). But his course is a good read and deals a lot with security.

    Don't worry about people stealing your ideas. If your ideas are any good, you'll have to ram them down people's throats.

    -Howard Aiken
Re: use CGI or die, even with mod_perl?
by Gilimanjaro (Hermit) on May 01, 2003 at 13:09 UTC
    Have a look at PLP by perlmonks very own Juerd...

    Integrates perfectly with mod_perl, performs like crazy and you'll never have to use CGI to access parameters or create headers, and you can stop using 'print'... :)