Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

cgi or mod_perl

by sberthold (Acolyte)
on Dec 24, 2005 at 02:35 UTC ( #518869=perlquestion: print w/replies, xml ) Need Help??
sberthold has asked for the wisdom of the Perl Monks concerning the following question:

I am starting to use perl to create some web pages. I have been doing a little research before I start. Should I be looking at CGI or mod_perl.

Replies are listed 'Best First'.
Re: cgi or mod_perl
by Thilosophy (Curate) on Dec 24, 2005 at 06:39 UTC
    A good approach would be to write your code so that it can run under both plain CGI and mod_perl (or FastCGI or SpeedyCGI or PPerl for that matter).

    Basically, this only means that you show some extra discipline concerning global variables, proper namespacing and such. Using a framework like CGI::Application takes you a long way here.

    This way, you can have the extreme availability and ease-of-installation of pure CGI and if your site gets popular enough for performance issues to come into play, you can switch over to mod_perl. The code will continue to work (and be much faster, mainly due to eliminated startup time)

    Making extensive use of mod_perl specific features (that would tie you into this architecture) is probably a lot of fun, but I do not think it is really worth the loss of flexibility.

Re: cgi or mod_perl
by bradcathey (Prior) on Dec 24, 2005 at 04:19 UTC

    Personally, I've only used CGI for my web development. My understanding is that mod_perl is better suited for larger sites with higher bandwidths because it not interpreted as straight CGI is (I'm sure I'll get some interesting replies and -- for that lame explanation). It runs faster and leaner as a result.

    If you are just beginning, you might want to get your feet wet with straight Perl and CGI (be sure to take Ovid's classic course). You'll learn the basics, most of which can be applied to mod_perl once you make the switch.

    I highly recommend you adapt a templating system early on. I use HTML::Template which is widely supported here at the Monastery. Also consider CGI::Application which will force you to write more structured code, and will lighten some of the coding burden.

    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: cgi or mod_perl
by venk (Acolyte) on Dec 24, 2005 at 14:37 UTC
    You might want to have a look at Catalyst (, a very fine web development framework IMHO. With Catalyst, I'm able to write my application at an abstraction level that makes the question of cgi vs mod_perl vs fastcgi rather less important, since I can quickly switch back and forth between any of them.
      ++ Catalyst is the best :)
Re: cgi or mod_perl
by integral (Hermit) on Dec 24, 2005 at 10:51 UTC

    I server quite a few CGI scripts from a fairly slow server, and the problem is that although perl scripts will run very fast, it's very slow to create new processes, initialise perl, and load all the modules before my script starts its work.

    mod_perl solves this one by having all the modules loaded in advanced, and it's registry modules can cache compiled scripts in memory. But it's rather complicated, and your memory usage can get out of hand if you aren't watching, so I tried FastCGI. It's much simpler.

    Rather than loading the script into apache, it creates a process for the script a lot like CGI, but it then just keeps on reusing it rather than discarding it after one use. The other advantage is that using CGI::Fast it's easy to convert your CGI scripts to FastCGI.

    So my suggestion is that if you're starting out with Perl is to go with CGI since it's simpler, and you do have room to expand to mod_perl or FastCGI later.

    integral, resident of freenode's #perl
Re: cgi or mod_perl
by astaines (Curate) on Dec 24, 2005 at 10:12 UTC

    A good question, but, at least partly, the wrong question. CGI is a useful low-level suite of tools for emitting HTML. It will happily emit HTML to a file, to a screen, or to a web server. Apache will display the output from programs written with CGI, but you need a perl interpreter, either your normal one (/usr/bin/perl or whatever), or one built into Apache.

    mod_perl is a very different animal, it is, specifically, a perl interpreter embedded into the Apache webserver. As such it doesn't do anything unless and until you feed it.

    CGI is quite a good choice for feeding mod_perl, athough not the only one. It is also easy to learn, and once you understand how CGI works, learning other frameworks will be significantly easier. mod_perl can do stuff that CGI can't, but you won't get near that stuff to start with.

    My own 2 cents - start with CGI and the perl interpreter on your system if you're using Linux. In Windows go with mod_perl from day 1, only because it's a bit easier to setup. If your Apache has mod_perl set up, use it. You normally only need mod_perl if your site gets very popular.

    Have fun, and a Happy Christmas to all

    -- Anthony Staines
Re: cgi or mod_perl
by pileofrogs (Priest) on Dec 24, 2005 at 18:19 UTC

    ...and I'll add my two cents. Do you manage the web server you'll be using? If not, mod_perl might be out of the question, since it's part of the web server.

    You can use the CGI module without having total control of the web server, so my vote says concentrate on that.

    Also, while mod_perl will make your code faster in a few situations, will make your code better in almost all situations. Go play with

    Performance is cool, but good code is cooler.


      Note that using cgi is not the same as using the module. The main difference between cgi and mod_perl is that for mod_perl the perl interpreter is embedded in the Apache process.

      When writing/testing a web application, one advantage of cgi over mod_perl is that because cgi reloads your modules for each request, you can easily make changes without having to restart the server. mod_perl does have some tricks to get around that though.

      The module helps make using cgi on a server easier, it can parse POST parameters, bake cookies, and generate compliant HTML code. With mod_perl some of that functionality is provided by the Apache:: modules, but you can use some parts of in mod_perl as well. So by all means, whether you use cgi or mod_perl, you should learn how to work with
Re: cgi or mod_perl
by gam3 (Curate) on Dec 25, 2005 at 04:54 UTC
    You don't have to make a choice if you are just creating simple web pages. The secret is to write all your web pages as modules and then you can call them from either mod_perl or a cgi-bin.
    # here is the web page. package WebPageOne; sub new { bless {}, shifth; } sub html { print <<EOP; <head> <body> This is a web page. </body> </head> } 1;
    And the cgi-bin
    #!/usr/bin/perl # webpageone.cgi use WebPageOne; print "content-type: text/html\n\n"; my $x = WebPageOne->new() print $x->html;
    And the mod_perl handler.
    package ModPerlPageOne; use Apache2::Const -compile => qw(OK); sub handler { my $r = shift; my $x = WebPageOne->new(); $r->send_http_header('text/html'); $r->print $x->html; return OK; }
    Of course these examples are missing most of the extra stuff you get from CGI and Apache, but normally all that is needed is CGI->param() and that can be hand rolled. Or you can pass in an instance of Apache::CGI or CGI to WebPageOne and use it for your CGI needs.
    -- gam3
    A picture is worth a thousand words, but takes 200K.
Re: cgi or mod_perl
by TedPride (Priest) on Dec 24, 2005 at 17:04 UTC
    On a low traffic site, it doesn't really matter how efficient your scripts are. Use CGI. Or if you don't know Perl at all yet, you might do the unthinkable and learn some basic PHP, since PHP is easier to learn and runs more efficiently than CGI. You'll want to know both eventually anyway.
      No matter what your server load, cgi scripts can be a little sluggish IMHO, especially if they start opening up database connections and the like. If your traffic is low, your server will surely survive, but your users won't be impressed.
      You can also run PHP as CGI, and I wouldn't be so sure that CGI PHP runs more efficiently than CGI Perl. Basicaly any mod_something is more efficient than CGI something. Also I'm not so sure which one is easier to learn, but this is probably a question of preferences.
      Someday, people who know how to use computers will rule over those who don't. And there will be a special name for them: secretaries. -Dilbert quote
      "since PHP ... runs more efficiently than CGI"

      Is that true? I did some test to sort 1 million numbers and Perl runs much faster than PHP. However, the PHP zealots said that's because Perl is optimized for loop code and PHP is not good at that. They also said that PHP is more effecient to generate web pages.

      Another question is that, I just thought PHP as a programming language like Perl. But CGI itself is not a language, so we can't compare PHP with CGI IMHO.

mod_perl may be overkill for simple or pedagogical apps
by skyknight (Hermit) on Dec 25, 2005 at 16:53 UTC
    If this is your first time using Perl, don't trouble yourself with the added complexity of mod_perl. This advice holds doubly so if you've never done web development previously. It can be a tricky environment, so there's no sense in overwhelming yourself initially. The mod_perl environment is great and I use it all the time, but if you're doing something simple whose aims are largely pedagogical, then you don't need the configuration niceties and run-time speed-up of mod_perl. That being said, if you're scrupulous about initializing stuff, then you should be able to migrate over to a mod_perl environment later when you need it.
Re: cgi or mod_perl
by ghosh (Initiate) on Dec 28, 2005 at 04:30 UTC
    Hi Regarding your option between cgi and mod_perl, as I see in your case cgi is the best option. You can differentiate between cgi and mod_perl only when you have good knowledge of functionality of cgi and apache. Moreover for small and medium websites you can hardly differentiate between the efficiency of cgi and mod_perl.
Re: cgi or mod_perl
by brittojs (Initiate) on Dec 24, 2005 at 20:23 UTC

    Considered by grinder: reap: empty (and has -ve rep)
    Unconsidered by Arunbear: keep votes prevented reaping; Keep: 5, Edit: 1, Reap: 20

      Given the choice, I usually opt for mod_perl. As mentioned above, CGI has a slower start-up time. If your application has high performance needs and you have full control of the server then I would suggest mod_perl. If it's a hosted server, or you don't measure your performance in milliseconds, then CGI will work just fine. The question you should be asking yourself is, what are the requirements of your application? That should dictate your decision.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://518869]
Approved by Joost
Front-paged by astaines
[Corion]: marto: Ah, yeah. If you can actually put Linux on them. I'm somewhat wary of the new Win10 Atom tablet thingies that are sold for ~EUR 280, and whether you can enter their UEFI at all to install Linux there. But that's sure worth a try.
[marto]: The ~60 Windows 10 intel based tablets would be a decent candidate
[Corion]: marto: Oh - yeah, that really sounds sensible

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2018-05-23 11:08 GMT
Find Nodes?
    Voting Booth?