1Plack::Component(3) User Contributed Perl Documentation Plack::Component(3)
2
3
4
6 Plack::Component - Base class for PSGI endpoints
7
9 package Plack::App::Foo;
10 use parent qw( Plack::Component );
11
12 sub call {
13 my($self, $env) = @_;
14 # Do something with $env
15
16 my $res = ...; # create a response ...
17
18 # return the response
19 return $res;
20 }
21
23 Plack::Component is the base class shared between Plack::Middleware and
24 "Plack::App::*" modules. If you are writing middleware, you should
25 inherit from Plack::Middleware, but if you are writing a Plack::App::*
26 you should inherit from this directly.
27
29 call ($env)
30 You are expected to implement a "call" method in your component.
31 This is where all the work gets done. It receives the PSGI $env
32 hash-ref as an argument and is expected to return a proper PSGI
33 response value.
34
36 new (%opts | \%opts)
37 The constructor accepts either a hash or a hashref and uses that to
38 create the instance. It will call no other methods and simply
39 return the instance that is created.
40
41 prepare_app
42 This method is called by "to_app" and is meant as a hook to be used
43 to prepare your component before it is packaged as a PSGI $app.
44
45 to_app
46 This is the method used in several parts of the Plack
47 infrastructure to convert your component into a PSGI $app. You
48 should not ever need to override this method; it is recommended to
49 use "prepare_app" and "call" instead.
50
51 response_cb
52 This is a wrapper for "response_cb" in Plack::Util. See "RESPONSE
53 CALLBACK" in Plack::Middleware for details.
54
56 Objects for the derived classes (Plack::App::* or Plack::Middleware::*)
57 are created at the PSGI application compile phase using "new",
58 "prepare_app" and "to_app", and the created object persists during the
59 web server lifecycle, unless it is running on the non-persistent
60 environment like CGI. "call" is invoked against the same object
61 whenever a new request comes in.
62
63 You can check if it is running in a persistent environment by checking
64 "psgi.run_once" key in the $env being true (non-persistent) or false
65 (persistent), but it is best for you to write your middleware safely
66 for a persistent environment. To accomplish that, you should avoid
67 saving per-request data like $env in your object.
68
70 The Plack::Middleware module used to inherit from
71 Class::Accessor::Fast, which has been removed in favor of the
72 Plack::Util::Accessor module. When developing new components it is
73 recommended to use Plack::Util::Accessor like so:
74
75 use Plack::Util::Accessor qw( foo bar baz );
76
77 However, in order to keep backwards compatibility this module provides
78 a "mk_accessors" method similar to Class::Accessor::Fast. New code
79 should not use this and use Plack::Util::Accessor instead.
80
82 Plack Plack::Builder Plack::Middleware
83
84
85
86perl v5.36.0 2022-09-12 Plack::Component(3)