Feersum::Connection::Handle(3pm) | User Contributed Perl Documentation | Feersum::Connection::Handle(3pm) |
Feersum::Connection::Handle - PSGI-style reader/writer objects.
For read handles:
my $buf; my $r = delete $env{'psgi.input'}; $r->read($buf, 1, 1); # read the second byte of input without moving offset $r->read($buf, $env{CONTENT_LENGTH}); # append the whole input $r->close(); # discards any un-read() data # assuming the handle is "open": $r->seek(2,SEEK_CUR); # returns 1, discards skipped bytes $r->seek(-1,SEEK_CUR); # returns 0, can't seek back # not yet supported, throws exception: # $r->poll_cb(sub { .... });
For write handles:
$w->write("scalar"); $w->write(\"scalar ref"); $w->write_array(\@some_stuff); $w->poll_cb(sub { # use $_[0] instead of $w to avoid a closure $_[0]->write(\"some data"); # can close() or unregister the poll_cb in here $_[0]->close(); });
For both:
$h->response_guard(guard { response_is_complete() });
See the PSGI spec for more information on how read/write handles are used (The Delayed Response and Streaming Body section has details on the writer).
The reader is obtained via "$env->{'psgi.input'}".
The calls to "$r->read()" will never block. Currently, the entire body is read into memory (or perhaps to a temp file) before the Feersum request handler is even called. This behaviour MAY change. Regardless, Feersum will be doing some buffering so "psgix.input.buffered" is set in the PSGI env hash.
$r->seek(0,SEEK_CUR); # returns 1 $r->seek(-1,SEEK_CUR); # returns 0 $r->seek(-1,SEEK_SET); # returns 0 $r->seek(2,SEEK_CUR); # returns 1, discards skipped bytes $r->seek(42,SEEK_SET); # returns 1 if room, discards skipped bytes $r->seek(-8,SEEK_END); # returns 1 if room, discards skipped bytes
The writer is obtained under PSGI by sending a code/headers pair to the "starter" callback. Under Feersum, calls to "$req->start_streaming" return one.
The calls to "$w->write()" will never block and data is buffered until transmitted. This behaviour is indicated by "psgix.output.buffered" in the PSGI env hash (Twiggy supports this too, for example).
A reference to the writer is passed in as the first and only argument to the sub. It's recommended that you use $_[0] rather than closing-over on $w to prevent a circular reference.
Methods in common to both types of handles.
The guard is *not* attached to this handle object; the guard is attached to the response.
"psgix.output.guard" is the PSGI-env extension that indicates this method.
Jeremy Stashewsky, "stash@cpan.org"
Copyright (C) 2010 by Jeremy Stashewsky & Socialtext Inc.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.7 or, at your option, any later version of Perl 5 you may have available.
2022-12-04 | perl v5.36.0 |