Plack::Component(3pm) | User Contributed Perl Documentation | Plack::Component(3pm) |
Plack::Component - Base class for PSGI endpoints
package Plack::App::Foo; use parent qw( Plack::Component ); sub call { my($self, $env) = @_; # Do something with $env my $res = ...; # create a response ... # return the response return $res; }
Plack::Component is the base class shared between Plack::Middleware and "Plack::App::*" modules. If you are writing middleware, you should inherit from Plack::Middleware, but if you are writing a Plack::App::* you should inherit from this directly.
Objects for the derived classes (Plack::App::* or Plack::Middleware::*) are created at the PSGI application compile phase using "new", "prepare_app" and "to_app", and the created object persists during the web server lifecycle, unless it is running on the non-persistent environment like CGI. "call" is invoked against the same object whenever a new request comes in.
You can check if it is running in a persistent environment by checking "psgi.run_once" key in the $env being true (non-persistent) or false (persistent), but it is best for you to write your middleware safely for a persistent environment. To accomplish that, you should avoid saving per-request data like $env in your object.
The Plack::Middleware module used to inherit from Class::Accessor::Fast, which has been removed in favor of the Plack::Util::Accessor module. When developing new components it is recommended to use Plack::Util::Accessor like so:
use Plack::Util::Accessor qw( foo bar baz );
However, in order to keep backwards compatibility this module provides a "mk_accessors" method similar to Class::Accessor::Fast. New code should not use this and use Plack::Util::Accessor instead.
Plack Plack::Builder Plack::Middleware
2018-02-14 | perl v5.26.1 |