[Catalyst-dev] [RFC PATCH] Catalyst::Request->print() method

Dagfinn Ilmari Mannsåker ilmari at ilmari.org
Sun Sep 7 22:56:09 BST 2008


Hi all,

Lots of modules have methods/functions that expect a filehandle to
->print() their results to; it would be handy to be able to pass them
$c->response and have it DTRT. Here's a patch (against 5.70/trunk)
that implements this, feedback welcome.

=== Changes
==================================================================
--- Changes	(revision 59039)
+++ Changes	(local)
@@ -4,6 +4,7 @@
         - Fix some Win32 test failures
         - Add pt translation of error message (wreis)
         - Make :Chained('../action') work (Florian Ragwitz)
+        - Add Catalyst::Response->print() method (ilmari)
 
 5.7099_03 2008-07-20 10:10:00
         - Fix regressions for regexp fallback in model(), view() and controller()
=== lib/Catalyst/Response.pm
==================================================================
--- lib/Catalyst/Response.pm	(revision 59039)
+++ lib/Catalyst/Response.pm	(local)
@@ -147,6 +147,27 @@
 
 sub write { shift->{_context}->write(@_); }
 
+=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
=== lib/Catalyst.pm
==================================================================
--- lib/Catalyst.pm	(revision 59039)
+++ lib/Catalyst.pm	(local)
@@ -2465,6 +2465,8 @@
 
 Geoff Richards
 
+ilmari: Dagfinn Ilmari Mannsåker <ilmari at ilmari.org>
+
 jcamacho: Juan Camacho
 
 Jody Belka
=== t/live_engine_response_print.t
==================================================================
--- t/live_engine_response_print.t	(revision 59039)
+++ t/live_engine_response_print.t	(local)
@@ -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" );
+}


-- 
ilmari
"A disappointingly low fraction of the human race is,
 at any given time, on fire." - Stig Sandbeck Mathisen



More information about the Catalyst-dev mailing list