Plack::Response(3)

πŸ’– Plack::Response πŸ’–

okay bestie, so this is the manual page for Plack::Response β€” like, the portable HTTP Response object for PSGI response one, from the User Contributed Perl Documentation corner of the world. sit down, grab a strawberry, we're doing this πŸ“βœ¨


✨ NAME ✨

Plack::Response β€” Portable HTTP Response object for PSGI response. that's it, that's the whole vibe. πŸ‘‘


🌈 SYNOPSIS 🌈

here's the little glow-up in action, no cap:

use Plack::Response;

sub psgi_handler {
    my $env = shift;

    my $res = Plack::Response->new(200);
    $res->content_type('text/html');
    $res->body("Hello World");

    return $res->finalize;
}

like… look at her. she's serving. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

so Plack::Response gives you a way to build a PSGI response array ref through a simple API. that's literally the whole deal. no drama, no cryptic nonsense, just vibes and a response ref. ✨


πŸŽ€ METHODS πŸŽ€

πŸ’« new

$res = Plack::Response->new;
$res = Plack::Response->new($status);
$res = Plack::Response->new($status, $headers);
$res = Plack::Response->new($status, $headers, $body);

Creates a new Plack::Response object. it's giving main character energy, it's giving birth of a response. 🫢 you can hand her nothing, or a status, or a status plus headers, or a status plus headers plus body β€” she's flexible like that. 🌸

πŸ’« status

$res->status(200);
$status = $res->status;

Sets and gets the HTTP status code. code is an alias, so if you're feeling quirky you can call her that instead. iykyk. 😌

πŸ’« headers

$headers = $res->headers;
$res->headers([ 'Content-Type' => 'text/html' ]);
$res->headers({ 'Content-Type' => 'text/html' });
$res->headers( HTTP::Headers::Fast->new );

Sets and gets the HTTP headers of the response. the setter can take either an array ref, a hash ref, or an HTTP::Headers::Fast object containing a list of headers. three options, all iconic, pick your fighter. 🧚

πŸ’« body

$res->body($body_str);
$res->body([ "Hello", "World" ]);
$res->body($io);

Gets and sets the HTTP response body. the setter can take either a string, an array ref, or an IO::Handle-like object. content is an alias, mwah. πŸ’‹

⚠️ okay so this is important, bestie: this method does not automatically set Content-Length for the response. you have to set it manually if you want to, with the content_length method (see below). she's not gonna do it for you, that's a you job. 😭
$res->header('X-Foo' => 'bar');
my $val = $res->header('X-Foo');

Shortcut for $res->headers->header. tiny little convenience, and honestly? we're obsessed. ✨

πŸ’« content_type, content_length, content_encoding

$res->content_type('text/plain');
$res->content_length(123);
$res->content_encoding('gzip');

Shortcut for the equivalent get/set methods in $res->headers. three lil besties doing the header work so you don't have to type so much. πŸŽ€

πŸ’« redirect

$res->redirect($url);
$res->redirect($url, 301);

Sets the redirect URL with an optional status code, which defaults to 302. bye bye, off you go, we're redirecting. πŸ¦‹

⚠️ note that this method doesn't normalize the given URI string. users of this module have to be responsible about properly encoding URI paths and parameters. so like… that's on you, boo. fr fr. πŸ™ˆ

πŸ’« location

Gets and sets the Location header.

⚠️ note that this method doesn't normalize the given URI string in the setter. see above in redirect for details. same energy, same warning. πŸ’€

πŸ’« cookies

$res->cookies->{foo} = 123;
$res->cookies->{foo} = { value => '123' };

Returns a hash reference containing cookies to be set in the response. the keys of the hash are the cookies' names, and their corresponding values are a plain string (for value with everything else defaults) or a hash reference that can contain keys such as value, domain, expires, path, httponly, secure, max-age. πŸ“

expires can take a string or an integer (as an epoch time) and does not convert string formats such as "+3M". she said what she said. 🌸

$res->cookies->{foo} = {
    value => 'test',
    path  => "/",
    domain => '.example.com',
    expires => time + 24 * 60 * 60,
};

look at that cookie being built. gorgeous. ✨

πŸ’« finalize

$res->finalize;

Returns the status code, headers, and body of this response as a PSGI response array reference. this is the moment. the finale. the finale finale. πŸ‘‘πŸŒˆ

πŸ’« to_app

$app = $res->to_app;

A helper shortcut for sub { $res->finalize }. tiny wrapper, big slay. πŸ’…


πŸ’œ AUTHOR πŸ’œ

Tokuhiro Matsuno

Tatsuhiko Miyagawa

legends, both of them, no notes. πŸ‘‘


🌈 SEE ALSO 🌈

  • Plack::Request β€” the other half of the duo, go say hi 🫢

and that's the tea, bestie πŸ’– this was Plack::Response(3), from perl v5.42.3, dated 2026-10-18, served to you fresh in the User Contributed Perl Documentation section. go build a response, you're gonna eat. periodt. πŸŽ€βœ¨πŸ¦‹


πŸ“¦ where this page comes from