Hello, Perl monks. I was able to write and run a PSGI middleware that validates a form. However, the problem is that I am not sure whether I did it correctly. What I did is that I mapped the middleware, $form_validator, instead of the wrapped PSGI application, $greeting. My code below will display a simple HTML form with a field name 'Taste of Vinegar' and a submit button. When you type in 'sour' and submit the form, the middleware, $form_validator, will be invoked to perform the validation. If all is well, then the PSGI application, $greeting, will be invoked. I am new to Perl/PSGI and I am not sure if I'm doing this correctly. I'd appreciate all of your comments on this. Best regards. :)

#!/usr/bin/perl use strict; use warnings; use Plack::Request; use Plack::Response; my $greeting = sub { return [ '200', [ 'Content-Type' => 'text/html' ], [ "<html> <body> <p>Congratulations! Your answe +r is correct!</p> </body> </html>"], ]; }; my $form_validator = sub { my $env = shift; # PSGI en +v my $request = Plack::Request->new($env); # new Pla +ck::Request my $taste = $request->param('taste'); if ($taste eq 'sour') { return $greeting->($env); } else { my $response = $request->new_response(200);; # + new Plack::Response $response->headers({ 'Content-Type' => 'text/html' }) +; $response->content('<html><body><p>Wrong answer!</p></ +body></html>' ); return $response->finalize; } }; my $form = sub { return [ 200, ['Content-Type' => 'text/html'], ["<html> <body> <form action='submit' method='GET'> Taste of Vinegar <input type='text' name=' +taste'/> <input type='submit' value='SUBMIT' /> </form> </body> </html>"] ]; }; my %routing = ( '/submit' => $form_validator, '/' => $form, ); my $app = sub { my $env = shift; # PSGI env my $request = Plack::Request->new($env); my $route = $routing{$request->path_info}; if ($route) {return $route->($env);} else { return [ '404', [ 'Content-Type' => 'text/html' ], [ '404 Not Found' ], ]; } };

In reply to Is it required to map a Plack/PSGI middleware? by tiny_monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.