CGI-Alternatives

 view release on metacpan or  search on metacpan

lib/CGI/Alternatives.pm  view on Meta::CPAN

Use L<Dancer2> rather than the original L<Dancer>, as Dancer2 solved many
architectural issues and is actively maintained. Dancer2 is known for its
simplicity and elegant syntax, making it perfect for small to medium applications.

Key features include: simple DSL, plugin system, session management, template
support, deployment flexibility via PSGI.

    #!/usr/bin/env perl

    # automatically enables strict and warnings
    use Dancer2;

    any [ 'get','post' ] => '/example_form' => sub {

        template 'example_form.html.tt', {
            'result' => params->{'user_input'}
        };
    };

    start;

Honestly that's just beautiful. The above example can be run with:

    perl examples/dancer2.pl

That makes the page available at http://*:3000/example_form

For production deployment with a proper PSGI server:

    plackup -s Starman bin/app.psgi

=head1 Catalyst

CPAN: L<https://metacpan.org/release/Catalyst-Runtime>

Repo: L<https://github.com/perl-catalyst/catalyst-runtime>

Home: L<https://www.catalystframework.org/>

Catalyst is one of the older web frameworks in perl, but is still actively
maintained and feature rich. It has a heavier dependency list than
the above frameworks, but this should not be taken as a negative point.
Catalyst is best suited for large, complex applications where you need
maximum flexibility and power.

Catalyst is slightly more involved in that you have to set up your entire app
as the first step, this involved running:

    catalyst.pl example_form

Which will create the various directories and scripts for building/running your
app. You then need to add the necessary controllers, views, and templates. This
has all been done automatically through the use of the helper scripts that come
with Catalyst. The important bit, the actual example code, is just this in the
examples/example_form/lib/example_form/Controller/Root.pm controller:

    package example_form::Controller::Root;

    # automatically enables strict and warnings
    use Moose;
    use namespace::autoclean;

    BEGIN { extends 'Catalyst::Controller' }

    __PACKAGE__->config(namespace => '');

    sub example_form : Local {

        my ( $self,$c ) = @_;

        $c->stash(
            template => 'example_form.html.tt',
            result   => $c->req->params->{user_input},
        );
    }

    sub end : ActionClass('RenderView') {}

    __PACKAGE__->meta->make_immutable;

    1;

Then running the server:

    perl examples/example_form/script/example_form_server.pl

Again makes the page available at http://*:3000/example_form

=head1 PSGI/Plack

Raw Plack is lower-level than Mojolicious so the code will be more verbose,
but Plack is probably a closer match to CGI.pm in terms of the things you're
having to handle.

PSGI: L<https://metacpan.org/release/PSGI>

Plack: L<https://metacpan.org/release/Plack>

Home: L<https://plackperl.org/>

PSGI is an interface between Perl web applications and web servers, and Plack
is a Perl module and toolkit that contains PSGI middleware, helpers and
adapters to web servers.

Plack is a collection of building blocks to create web applications, ranging from
quick & easy scripts, to the foundations of building larger frameworks. All modern
Perl web frameworks (Mojolicious, Dancer2, Catalyst) are built on PSGI/Plack.

=head2 Plack As A Persistent Process

    #!/usr/bin/env perl

    use strict;
    use warnings;
    use feature qw/ state /;

    use FindBin qw/ $Bin /;
    use Template;
    use Plack::Request;
    use Plack::Response;



( run in 3.007 seconds using v1.01-cache-2.11-cpan-98e64b0badf )