Plack::Util - Utility subroutines for Plack server and framework developers
my $true = Plack::Util::TRUE; my $false = Plack::Util::FALSE;
Utility constants to include when you specify boolean variables in $env hash (e.g. psgi.multithread).
$env
psgi.multithread
my $class = Plack::Util::load_class($class [, $prefix ]);
Constructs a class name and require the class. Throws an exception if the .pm file for the class is not found, just with the built-in require.
require
If $prefix is set, the class name is prepended to the $class unless $class begins with + sign, which means the class name is already fully qualified.
$prefix
$class
+
my $class = Plack::Util::load_class("Foo"); # Foo my $class = Plack::Util::load_class("Baz", "Foo::Bar"); # Foo::Bar::Baz my $class = Plack::Util::load_class("+XYZ::ZZZ", "Foo::Bar"); # XYZ::ZZZ
if ( Plack::Util::is_real_fh($fh) ) { }
returns true if a given $fh is a real file handle that has a file descriptor. It returns false if $fh is PerlIO handle that is not really related to the underlying file etc.
$fh
my $cl = Plack::Util::content_length($body);
Returns the length of content from body if it can be calculated. If $body is an array ref it's a sum of length of each chunk, if $body is a real filehandle it's a remaining size of the filehandle, otherwise returns undef.
$body
Plack::Util::set_io_path($fh, "/path/to/foobar.txt");
Sets the (absolute) file path to $fh filehandle object, so you can call $fh->path on it. As a side effect $fh is blessed to an internal package but it can still be treated as a normal file handle.
$fh->path
This module doesn't normalize or absolutize the given path, and is intended to be used from Server or Middleware implementations. See also IO::File::WithPath.
Plack::Util::foreach($body, $cb);
Iterate through $body which is an array reference or IO::Handle-like object and pass each line (which is NOT really guaranteed to be a line) to the callback function.
It internally sets the buffer length $/ to 4096 in case it reads the binary file, unless otherwise set in the caller's code.
$/
my $app = Plack::Util::load_psgi $app_psgi;
Load app.psgi file and evaluate the code to get PSGI application handler. If the file can't be loaded (e.g. file doesn't exist or has a perl syntax error), it will throw an exception.
app.psgi
my $res = Plack::Util::run_app $app, $env;
Runs the $app by wrapping errors with eval and if an error is found, logs it to $env->{'psgi.errors'} and returns the template 500 Error response.
$env->{'psgi.errors'}
my $hdrs = [ 'Content-Type' => 'text/plain' ]; my $v = Plack::Util::header_get($hdrs, $key); # First found only my @v = Plack::Util::header_get($hdrs, $key); my $bool = Plack::Util::header_exists($hdrs, $key); Plack::Util::header_set($hdrs, $key, $val); # overwrites existent header Plack::Util::header_push($hdrs, $key, $val); Plack::Util::header_remove($hdrs, $key);
Utility functions to manipulate PSGI response headers array reference. The methods that read existent header value handles header name as case insensitive.
my $hdrs = [ 'Content-Type' => 'text/plain' ]; my $v = Plack::Util::header_get('content-type'); # 'text/plain'
my $headers = [ 'Content-Type' => 'text/plain' ]; my $h = Plack::Util::headers($headers); $h->get($key); if ($h->exists($key)) { ... } $h->set($key => $val); $h->push($key => $val); $h->remove($key); $h->headers; # same reference as $headers
Given a header array reference, returns a convenient object that has an instance methods to access header_* functions with an OO interface. The object holds a reference to the original given $headers argument and updates the reference accordingly when called write methods like set, push or remove. It also has headers method that would return the same reference.
header_*
$headers
set
push
remove
headers
if (status_with_no_entity_body($res->[0])) { }
Returns true if the given status code doesn't have any Entity body in HTTP response, i.e. it's 100, 101, 204 or 304.
my $o = Plack::Util::inline_object( write => sub { $h->push_write(@_) }, close => sub { $h->push_shutdown }, ); $o->write(@stuff); $o->close;
Creates an instant object that can react to methods passed in the constructor. Handy to create when you need to create an IO stream object for input or errors.
To install Plack, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Plack
CPAN shell
perl -MCPAN -e shell install Plack
For more information on module installation, please visit the detailed CPAN module installation guide.