Support print for Catalyst::Request
Marcus Ramberg [Mon, 8 Sep 2008 21:05:23 +0000 (21:05 +0000)]
Changes
lib/Catalyst.pm
lib/Catalyst/Response.pm
t/lib/TestApp/Controller/Engine/Response/Print.pm [new file with mode: 0644]
t/live_engine_response_print.t [new file with mode: 0644]

diff --git a/Changes b/Changes
index d898cec..d3abf54 100644 (file)
--- a/Changes
+++ b/Changes
@@ -4,7 +4,9 @@
         - Port to Moose
         - Added test for action stringify
         - Added test for component instances getting $self->{value} from config.
-        - Chained doc improvements (rev 8326-8328)
+        - Add Catalyst::Response->print() method (ilmari)
+         
+         ))
 
 5.7XXXXXX XXXX
         - Fix some Win32 test failures
index b0d30d7..38b8fa5 100644 (file)
@@ -2475,6 +2475,8 @@ Gary Ashton Jones
 
 Geoff Richards
 
+ilmari: Dagfinn Ilmari MannsÃ¥ker <ilmari@ilmari.org>
+
 jcamacho: Juan Camacho
 
 Jody Belka
index 545341d..8c5cc50 100644 (file)
@@ -164,6 +164,27 @@ Writes $data to the output stream.
 
 Provided by Moose
 
+=head2 $res->print( @data )
+
+Prints @data to the output stream, separated by $,.  This lets you pass
+the response object to functions that want to write to an L<IO::Handle>.
+
+=cut
+
+sub print {
+    my $self = shift;
+    my $data = shift;
+
+    defined $self->write($data) or return;
+
+    for (@_) {
+        defined $self->write($,) or return;
+        defined $self->write($_) or return;
+    }
+    
+    return 1;
+}
+
 =head1 AUTHORS
 
 Catalyst Contributors, see Catalyst.pm
diff --git a/t/lib/TestApp/Controller/Engine/Response/Print.pm b/t/lib/TestApp/Controller/Engine/Response/Print.pm
new file mode 100644 (file)
index 0000000..1d1f52d
--- /dev/null
@@ -0,0 +1,25 @@
+package TestApp::Controller::Engine::Response::Print;
+
+use strict;
+use base 'Catalyst::Base';
+
+sub one :Relative {
+    my ( $self, $c ) = @_;
+    
+    $c->res->print("foo");
+}
+
+sub two :Relative {
+    my ( $self, $c ) = @_;
+
+    $c->res->print(qw/foo bar/);
+}
+
+sub three :Relative {
+    my ( $self, $c ) = @_;
+
+    local $, = ',';
+    $c->res->print(qw/foo bar baz/);
+}
+
+1;
diff --git a/t/live_engine_response_print.t b/t/live_engine_response_print.t
new file mode 100644 (file)
index 0000000..b6c6446
--- /dev/null
@@ -0,0 +1,24 @@
+#!perl
+
+use strict;
+use warnings;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use Test::More tests => 9;
+use Catalyst::Test 'TestApp';
+
+my $expected = {
+   one => "foo",
+   two => "foobar",
+   three => "foo,bar,baz",
+};
+
+for my $action ( keys %{$expected} ) {
+    ok( my $response = request('http://localhost/engine/response/print/' . $action ),
+        'Request' );
+    ok( $response->is_success, "Response $action successful 2xx" );
+    
+    is( $response->content, $expected->{$action}, "Content $action OK" );
+}