When should I use Perl CGI instead of PHP (or vice versa)?

前端 未结 8 1422
悲哀的现实
悲哀的现实 2021-01-07 23:48

For hobby purposes, I have a shared space on a hosting server that is providing, as many of them are, both PHP and Perl CGI. I have read on several places that CGI scripts

相关标签:
8条回答
  • 2021-01-08 00:24

    The "obsolete"-ness of CGI is really only a factor if you are doing big, complex sites with lots of page views.

    Many people push the idea that CGI is obsolete don't really understand what CGI is. There is a widespread misconception that CGI is an inherently Perl-based technology. Many people attack CGI as a way to pad out cultic attacks on Perl in support of whatever language they support. If you want to be a real technologist, you need to understand the fundamental issues and make a choice based on the facts of the situation.

    CGI is an interface with a webserver that allows you to write interactive pages in any language--even befunge. When a server gets a request for a page controlled by a CGI script, the server runs the script and returns the results to the requester.

    If your programming language requires a VM, interpreter or compiler to load each time it executes, then this start-up time will be required each time your page is accessed.

    CGI accelerators like FastCGI, mod_php, mod_perl and so forth, keep an interpreter/VM in memory at all times, may keep libraries loaded, and even cache bytecode from scripts to reduce script start-up overhead.

    If you are making a simple, personal or hobby site, CGI will be fine. So will PHP.

    If your site should grow to need a faster technology, you can move to mod_perl, FastCGI, or other CGI acceleration technologies.

    What language you use should be determined by the tools it provides and how they fit with your needs.

    1. Make a list of the capabilities you need.
    2. Make a list of deal breakers.
    3. Now check each of your possible toolsets against these two lists.
    4. Which one comes out the best? Test it.
    5. Does it suck? Cross it off your list, and go back to step 4.

    Also, I recommend against using befunge. Just because it is possible, it doesn't mean you should use it.


    Update: As mpeters points out, mod_perl, mod_php, mod_ruby, et alia are much more than mere CGI accelerators; they provide access to the Apache API. They act as CGI accelerators, but can do much, much, more.

    FastCGI is a pure CGI accelerator.

    Update 2: PHP and CGI are not mutually exclusive. PHP can be installed as a CGI. PHP is often used with FastCGI.

    0 讨论(0)
  • 2021-01-08 00:26

    Try Catalyst with Template Toolkit.

    sub hello :Path('/hello') :Args(0) {
        my ( $self, $c ) = @_;
    
        # Hello World
        $c->response->body( $c->welcome_message );
    }
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
    <html>
      <head>
        <title>[% title %]</title>
      </head>
      <body> 
        <div id="header">
          <a href="/index.html" class="logo" alt="Home Page"></a>
          <h1 class="headline">[% title %]</h1>
        </div>
    
        [% content %]
    
        <div id="footer">
          <div id="copyright">
            &copy; [% copyright %]
          </div>
        </div>
      </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题