Plack::Component(3)
π NAME
okay so bestie, welcome to Plack::Component π β it's the base class for PSGI endpoints. that's the whole vibe. the blueprint. the mother of all your lil app modules. periodt β¨
πΈ SYNOPSIS
here's the shape of it, copy this energy:
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;
}
likeβ¦ you inherit, you write call, you return a response. that's it. that's the whole arc. π¦
π DESCRIPTION
okay so Plack::Component is the base class that's shared between Plack::Middleware and Plack::App::* modules. so she's literally the common ancestor, the shared group chat, the mutual bestie. π
but vibe check: if you're writing middleware, you should inherit from Plack::Middleware instead. and if you're writing a Plack::App::*, you inherit from this one directly. don't mix it up bestie, it's giving wrong-parent-energy π
β REQUIRED METHOD
call ($env)β okay so this is the one you HAVE to implement in your component. this is where all the work gets done, no cap. it receives the PSGI$envhash-ref as an argument, and it's expected to return a proper PSGI response value. that's the deal. that's the assignment. slay π
π METHODS
new (%opts | \%opts)β the constructor! it accepts either a hash or a hashref and uses that to create the instance. and listen β it will call no other methods and will simply return the instance that is created. lowkey minimalist of it β¨prepare_appβ this one gets called byto_app, and it's meant as a hook you use to prepare your component before it gets packaged as a PSGI$app. think of it as getting ready before the photoshoot πto_appβ this is the method used in several parts of the Plack infrastructure to convert your component into a PSGI$app. and bestie, you should never ever need to override this one. it is recommended to useprepare_appandcallinstead. don't touch the machinery, it's not your moment πresponse_cbβ this is just a wrapper forresponse_cbin Plack::Util. for the details, see "RESPONSE CALLBACK" in Plack::Middleware. iykyk π¦
π§ OBJECT LIFECYCLE
okay so here's the timeline, and it's actually kind of iconic:
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's running on the non-persistent environment like CGI. and then call is invoked against the same object whenever a new request comes in. same object, new requests, main character energy, we love to see it π
you can check if it's running in a persistent environment by checking the psgi.run_once key in the $env being true (non-persistent) or false (persistent).
but bestie, the real tea: 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. do not do it. i am begging π«Ά
π BACKWARDS COMPATIBILITY
story time, gather round π
The Plack::Middleware module used to inherit from Class::Accessor::Fast, which has been removed in favor of the Plack::Util::Accessor module. so when you're 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 should use Plack::Util::Accessor instead. it's giving legacy, it's giving deprecated-adjacent, we respect her history but we do not date her π
β¨ SEE ALSO
- Plack
- Plack::Builder
- Plack::Middleware
go read them, they're all in the same friend group π¦
perl v5.42.3 π 2026-10-18 β that's the version and the date, straight from the Plack::Component(3) manual page footer π ok bye bestie, go write something slay π«Άβ¨
π¦ where this page comes from
- fedora 43: perl-Plack 1.0055-1.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- fedora 44: perl-Plack 1.0055-1.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms