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 $env hash-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 by to_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 use prepare_app and call instead. don't touch the machinery, it's not your moment πŸ™ˆ
  • response_cb β€” this is just a wrapper for response_cb in 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