Skip to content

Commit

Permalink
fixed streaming recipe in cookbook
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 19, 2013
1 parent cb9802c commit 47a4dbe
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,4 +1,6 @@

4.17 2013-06-20

4.16 2013-06-19
- Improved Perl 5.10.x and 5.12.x compatibility. (trinitum)

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -41,7 +41,7 @@ has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };

our $CODENAME = 'Top Hat';
our $VERSION = '4.16';
our $VERSION = '4.17';

sub AUTOLOAD {
my $self = shift;
Expand Down
18 changes: 9 additions & 9 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -891,24 +891,24 @@ Sending a streaming request is almost just as easy.
my $ua = Mojo::UserAgent->new;
my $tx = $ua->build_tx(GET => 'http://example.com');

# Prepare content
my $content = 'Hello world!';
$tx->req->headers->content_length(length $content);
# Prepare body
my $body = 'Hello world!';
$tx->req->headers->content_length(length $body);

# Start writing directly with a drain callback
my $drain;
$drain = sub {
my $req = shift;
my $chunk = substr $content, 0, 1, '';
$drain = undef unless length $content;
$req->write($chunk, $drain);
my $content = shift;
my $chunk = substr $body, 0, 1, '';
$drain = undef unless length $body;
$content->write($chunk, $drain);
};
$tx->req->$drain;
$tx->req->content->$drain;

# Process transaction
$ua->start($tx);

The drain callback passed to L<Mojo::Message/"write"> will be invoked whenever
The drain callback passed to L<Mojo::Content/"write"> will be invoked whenever
the entire previous chunk has actually been written.

=head2 Large file downloads
Expand Down

0 comments on commit 47a4dbe

Please sign in to comment.