It is a reference to a sub that expects a hash ref representing the HTTP request passed to it as the one and only parameter and which returns an array ref representing a HTTP response. Hmm - yeah that's what web apps do - take the request and return the response.
But then you think - OK, but I like Object Oriented code and I want my application to be an object with attributes and stuff - so what can I do? Well - that is simple. Have your app as object as you like, and to the PSGI layer pass a closure referring to that object. For example if you application object normally handles the requests with a ->handle method then
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub psgi_callback { | |
my $self = shift; | |
sub { | |
my $req = Plack::Request->new( shift ); | |
$self->handle( $req ); | |
}; | |
} |
would return a suitable callback. Which you can then use in your app.psgi file:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use MyApp; | |
my $app = MyApp->new(); | |
$app->psgi_callback; |
And this is exactly what I do in WebNano.
No comments:
Post a Comment