Permalink
Browse files

add example of Plack as a standalone script

this is a few lines to explain that Plack can be used as one off
(non-persistant) processes which may fit some use cases better than them
having to be concerned with deployment (such that they need to restart
an application server) and they just want to drop a script in a dir and
have apache/nginx/whatever exec it as a standalone CGI script

w/r/t to recent discussion on r/perl[1] and bpo[2]

bump VERSION and Changes for CPAN release

[1] https://www.reddit.com/r/perl/comments/9wfgtf/perl_and_cgi/
[2] http://blogs.perl.org/users/aristotle/2018/11/modern-perl-cgi.html
  • Loading branch information...
leejo committed Nov 18, 2018
1 parent a142db5 commit c3473c013a58b80fc841125cf07abc94d5981202
Showing with 150 additions and 111 deletions.
  1. +3 −0 Changes
  2. +73 −55 README.md
  3. +74 −56 lib/CGI/Alternatives.pm
@@ -1,5 +1,8 @@
Revision history for CGI::Alternatives
0.16 2018-11-18
- Add note about running Plack scripts as fire-and-forget (one off) CGI scripts
0.15 2016-10-13
- Fix typos (thanks to davorg)
128 README.md
@@ -10,7 +10,7 @@ CGI::Alternatives - Documentation for alternative solutions to CGI.pm
# VERSION
0.15
0.16
# DESCRIPTION
@@ -206,60 +206,6 @@ Please don't write your own template engine. If you want to completely split
out your html and still have some sort of templating system there are modules
to do that, such as [HTML::Zoom](https://metacpan.org/pod/HTML::Zoom).
# PSGI/Plack
[http://metacpan.org/release/PSGI](http://metacpan.org/release/PSGI)
[http://metacpan.org/release/Plack](http://metacpan.org/release/Plack)
[http://plackperl.org/](http://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.
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/ state /;
use FindBin qw/ $Bin /;
use Template;
use Plack::Request;
use Plack::Response;
my $app = sub {
my $req = Plack::Request->new( shift );
my $res = Plack::Response->new( 200 );
state $tt = Template->new({
INCLUDE_PATH => "$Bin/templates",
});
my $out;
$tt->process(
"example_form.html.tt",
{
result => $req->parameters->{'user_input'},
},
\$out,
) or die $tt->error;
$res->body( $out );
$res->finalize;
};
To run this script:
plackup examples/plack_psgi.pl
That makes the script (the "app") available at http://\*:5000
# Mojolicious
CPAN: [http://metacpan.org/release/Mojolicious](http://metacpan.org/release/Mojolicious)
@@ -459,6 +405,78 @@ Then running the server:
Again makes the page available at http://\*:3000/example\_form
# 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.
[http://metacpan.org/release/PSGI](http://metacpan.org/release/PSGI)
[http://metacpan.org/release/Plack](http://metacpan.org/release/Plack)
[http://plackperl.org/](http://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.
## Plack As A Peristant 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;
my $app = sub {
my $req = Plack::Request->new( shift );
my $res = Plack::Response->new( 200 );
state $tt = Template->new({
INCLUDE_PATH => "$Bin/templates",
});
my $out;
$tt->process(
"example_form.html.tt",
{
result => $req->parameters->{'user_input'},
},
\$out,
) or die $tt->error;
$res->body( $out );
$res->finalize;
};
To run this script:
plackup examples/plack_psgi.pl
That makes the script (the "app") available at http://\*:5000
## Plack As A Run On Demand CGI Script
If your CGI script only runs once in a while, and doesn't need to be persistant,
then you can use Plack the same way and not have to worry about deployment
concerns such has having to restart a process. To do so requires adding:
use Plack::Handler::CGI;

This comment has been minimized.

@davorg

davorg Nov 18, 2018

Contributor

I believe you can create exactly the same effect by just changing the shebang line to

#!/usr/bin/plackup

No need to add the Plack::Handler::CGI code.

This comment has been minimized.

@leejo

leejo Nov 18, 2018

Owner

Possibly - I'm going by the example by Aristotle in the blog post referenced in the commit message. I'll tweak the docs if it's the case, cheers!

This comment has been minimized.

@ap

ap Nov 18, 2018

You can, but I’d personally recommend against it, for the reasons I gave over at b.p.o.

Plack::Handler::CGI->new->run($app);
to the end of the script. This will allow it to be exec'd correctly by the
upfront webserver and to behave like a standalone CGI script
# Others
The three (four) examples above are the "big three", currently very popular
@@ -3,7 +3,7 @@ package CGI::Alternatives;
use strict;
use warnings;
our $VERSION = '0.15';
our $VERSION = '0.16';
1;
@@ -17,7 +17,7 @@ CGI::Alternatives - Documentation for alternative solutions to CGI.pm
=head1 VERSION
0.15
0.16
=head1 DESCRIPTION
@@ -213,60 +213,6 @@ Please don't write your own template engine. If you want to completely split
out your html and still have some sort of templating system there are modules
to do that, such as L<HTML::Zoom>.
=head1 PSGI/Plack
L<http://metacpan.org/release/PSGI>
L<http://metacpan.org/release/Plack>
L<http://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.
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/ state /;
use FindBin qw/ $Bin /;
use Template;
use Plack::Request;
use Plack::Response;
my $app = sub {
my $req = Plack::Request->new( shift );
my $res = Plack::Response->new( 200 );
state $tt = Template->new({
INCLUDE_PATH => "$Bin/templates",
});
my $out;
$tt->process(
"example_form.html.tt",
{
result => $req->parameters->{'user_input'},
},
\$out,
) or die $tt->error;
$res->body( $out );
$res->finalize;
};
To run this script:
plackup examples/plack_psgi.pl
That makes the script (the "app") available at http://*:5000
=head1 Mojolicious
CPAN: L<http://metacpan.org/release/Mojolicious>
@@ -466,6 +412,78 @@ Then running the server:
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.
L<http://metacpan.org/release/PSGI>
L<http://metacpan.org/release/Plack>
L<http://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.
=head2 Plack As A Peristant 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;
my $app = sub {
my $req = Plack::Request->new( shift );
my $res = Plack::Response->new( 200 );
state $tt = Template->new({
INCLUDE_PATH => "$Bin/templates",
});
my $out;
$tt->process(
"example_form.html.tt",
{
result => $req->parameters->{'user_input'},
},
\$out,
) or die $tt->error;
$res->body( $out );
$res->finalize;
};
To run this script:
plackup examples/plack_psgi.pl
That makes the script (the "app") available at http://*:5000
=head2 Plack As A Run On Demand CGI Script
If your CGI script only runs once in a while, and doesn't need to be persistant,
then you can use Plack the same way and not have to worry about deployment
concerns such has having to restart a process. To do so requires adding:
use Plack::Handler::CGI;
Plack::Handler::CGI->new->run($app);
to the end of the script. This will allow it to be exec'd correctly by the
upfront webserver and to behave like a standalone CGI script
=head1 Others
The three (four) examples above are the "big three", currently very popular

0 comments on commit c3473c0

Please sign in to comment.