1Plack::Util(3) User Contributed Perl Documentation Plack::Util(3)
2
3
4
6 Plack::Util - Utility subroutines for Plack server and framework
7 developers
8
10 TRUE, FALSE
11 my $true = Plack::Util::TRUE;
12 my $false = Plack::Util::FALSE;
13
14 Utility constants to include when you specify boolean variables in
15 $env hash (e.g. "psgi.multithread").
16
17 load_class
18 my $class = Plack::Util::load_class($class [, $prefix ]);
19
20 Constructs a class name and "require" the class. Throws an
21 exception if the .pm file for the class is not found, just with the
22 built-in "require".
23
24 If $prefix is set, the class name is prepended to the $class unless
25 $class begins with "+" sign, which means the class name is already
26 fully qualified.
27
28 my $class = Plack::Util::load_class("Foo"); # Foo
29 my $class = Plack::Util::load_class("Baz", "Foo::Bar"); # Foo::Bar::Baz
30 my $class = Plack::Util::load_class("+XYZ::ZZZ", "Foo::Bar"); # XYZ::ZZZ
31
32 is_real_fh
33 if ( Plack::Util::is_real_fh($fh) ) { }
34
35 returns true if a given $fh is a real file handle that has a file
36 descriptor. It returns false if $fh is PerlIO handle that is not
37 really related to the underlying file etc.
38
39 content_length
40 my $cl = Plack::Util::content_length($body);
41
42 Returns the length of content from body if it can be calculated. If
43 $body is an array ref it's a sum of length of each chunk, if $body
44 is a real filehandle it's a remaining size of the filehandle,
45 otherwise returns undef.
46
47 set_io_path
48 Plack::Util::set_io_path($fh, "/path/to/foobar.txt");
49
50 Sets the (absolute) file path to $fh filehandle object, so you can
51 call "$fh->path" on it. As a side effect $fh is blessed to an
52 internal package but it can still be treated as a normal file
53 handle.
54
55 This module doesn't normalize or absolutize the given path, and is
56 intended to be used from Server or Middleware implementations. See
57 also IO::File::WithPath.
58
59 foreach
60 Plack::Util::foreach($body, $cb);
61
62 Iterate through $body which is an array reference or
63 IO::Handle-like object and pass each line (which is NOT really
64 guaranteed to be a line) to the callback function.
65
66 It internally sets the buffer length $/ to 4096 in case it reads
67 the binary file, unless otherwise set in the caller's code.
68
69 load_psgi
70 my $app = Plack::Util::load_psgi $psgi_file_or_class;
71
72 Load "app.psgi" file or a class name (like "MyApp::PSGI") and
73 require the file to get PSGI application handler. If the file can't
74 be loaded (e.g. file doesn't exist or has a perl syntax error), it
75 will throw an exception.
76
77 Security: If you give this function a class name or module name
78 that is loadable from your system, it will load the module. This
79 could lead to a security hole:
80
81 my $psgi = ...; # user-input: consider "Moose.pm"
82 $app = Plack::Util::load_psgi($psgi); # this does 'require "Moose.pm"'!
83
84 Generally speaking, passing an external input to this function is
85 considered very insecure. But if you really want to do that, be
86 sure to validate the argument passed to this function. Also, if you
87 do not want to accept an arbitrary class name but only load from a
88 file path, make sure that the argument $psgi_file_or_class begins
89 with "/" so that Perl's built-in do function won't search the
90 include path.
91
92 run_app
93 my $res = Plack::Util::run_app $app, $env;
94
95 Runs the $app by wrapping errors with eval and if an error is
96 found, logs it to "$env->{'psgi.errors'}" and returns the template
97 500 Error response.
98
99 header_get, header_exists, header_set, header_push, header_remove
100 my $hdrs = [ 'Content-Type' => 'text/plain' ];
101
102 my $v = Plack::Util::header_get($hdrs, $key); # First found only
103 my @v = Plack::Util::header_get($hdrs, $key);
104 my $bool = Plack::Util::header_exists($hdrs, $key);
105 Plack::Util::header_set($hdrs, $key, $val); # overwrites existent header
106 Plack::Util::header_push($hdrs, $key, $val);
107 Plack::Util::header_remove($hdrs, $key);
108
109 Utility functions to manipulate PSGI response headers array
110 reference. The methods that read existent header value handles
111 header name as case insensitive.
112
113 my $hdrs = [ 'Content-Type' => 'text/plain' ];
114 my $v = Plack::Util::header_get($hdrs, 'content-type'); # 'text/plain'
115
116 headers
117 my $headers = [ 'Content-Type' => 'text/plain' ];
118
119 my $h = Plack::Util::headers($headers);
120 $h->get($key);
121 if ($h->exists($key)) { ... }
122 $h->set($key => $val);
123 $h->push($key => $val);
124 $h->remove($key);
125 $h->headers; # same reference as $headers
126
127 Given a header array reference, returns a convenient object that
128 has an instance methods to access "header_*" functions with an OO
129 interface. The object holds a reference to the original given
130 $headers argument and updates the reference accordingly when called
131 write methods like "set", "push" or "remove". It also has "headers"
132 method that would return the same reference.
133
134 status_with_no_entity_body
135 if (status_with_no_entity_body($res->[0])) { }
136
137 Returns true if the given status code doesn't have any Entity body
138 in HTTP response, i.e. it's 100, 101, 204 or 304.
139
140 inline_object
141 my $o = Plack::Util::inline_object(
142 write => sub { $h->push_write(@_) },
143 close => sub { $h->push_shutdown },
144 );
145 $o->write(@stuff);
146 $o->close;
147
148 Creates an instant object that can react to methods passed in the
149 constructor. Handy to create when you need to create an IO stream
150 object for input or errors.
151
152 response_cb
153 See "RESPONSE CALLBACK" in Plack::Middleware for details.
154
155
156
157perl v5.12.3 2011-07-19 Plack::Util(3)