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

前端 未结 8 1427
悲哀的现实
悲哀的现实 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:13

    Here is a simple "hello world" example that runs under CGI using the Squatting web microframework:

    use strict;
    use warnings;
    
    {
        package MyApp;
        use base 'Squatting';
        use base 'Squatting::On::CGI';
    }
    
    { 
        package MyApp::Controllers;
        use Squatting ':controllers';
    
        our @C = (
            C(
                Index => [ '/' ],
                get   => sub { 
                    my ( $self ) = @_;
                    my $v = $self->v;
                    $v->{say} = 'hello world!';
                    $self->render( 'hello' );
                },
            ),
        );
    }
    
    {
        package MyApp::Views;
        use Squatting ':views';
        use HTML::AsSubs;
    
        our @V = (
            V(  'html',
    
                layout => sub { 
                    my ( $self, $v, @yield ) = @_;
                    html (
                        head ( title( 'My CGI App' ) ),
                        body ( @yield ),
                    )->as_HTML;
                },
    
                hello => sub {
                    my ( $self, $v ) = @_;
                    p ( $v->{say} );
                },
            ),
        );
    }
    
    use CGI;
    my $q = CGI->new;
    MyApp->init;
    MyApp->relocate('/cgi-bin/myapp.cgi');
    MyApp->cgi($q);
    

    Save as "myapp.cgi" and place in your cgi-bin.

提交回复
热议问题