Added body_ref and body_length and minor optimization, use refs where it's possible
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Response.pm
1 package Catalyst::Response;
2
3 use strict;
4 use base 'Class::Accessor::Fast';
5
6 __PACKAGE__->mk_accessors(qw/cookies body headers redirect status/);
7
8 *output = \&body;
9
10 sub content_encoding { shift->headers->content_encoding(@_) }
11 sub content_length   { shift->headers->content_length(@_)   }
12 sub content_type     { shift->headers->content_type(@_)     }
13 sub header           { shift->headers->header(@_)           }
14
15 =head1 NAME
16
17 Catalyst::Response - Catalyst Response Class
18
19 =head1 SYNOPSIS
20
21     $resp = $c->response;
22     $resp->body;
23     $resp->body_length;
24     $resp->body_ref;
25     $resp->content_encoding;
26     $resp->content_length;
27     $resp->content_type;
28     $resp->cookies;
29     $resp->header;
30     $resp->headers;
31     $resp->output;
32     $resp->redirect;
33     $resp->status;
34
35 See also L<Catalyst::Application>.
36
37 =head1 DESCRIPTION
38
39 This is the Catalyst Response class, which provides a set of accessors
40 to response data.
41
42 =head1 METHODS
43
44 =over 4
45
46 =item $resp->body($text)
47
48     $c->response->body('Catalyst rocks!');
49
50 Contains the final output.
51
52 =item $resp->body_length
53
54 Returns the length of body in bytes.
55
56     print $c->response->body_length
57
58 =cut
59
60 sub body_length {
61     my $self = shift;
62     
63     use bytes;
64     
65     return 0 unless $self->body;
66     return length($self->body);
67 }
68
69 =item $resp->body_ref
70
71 Returns a reference to body.
72
73 =cut
74
75 sub body_ref {
76     my $self = shift;    
77     return \$self->{body};
78 }
79
80 =item $resp->content_encoding
81
82 Shortcut to $resp->headers->content_encoding
83
84 =item $resp->content_length
85
86 Shortcut to $resp->headers->content_length
87
88 =item $resp->content_type
89
90 Shortcut to $resp->headers->content_type
91
92 =item $resp->cookies
93
94 Returns a reference to a hash containing the cookies to be set.
95
96     $c->response->cookies->{foo} = { value => '123' };
97
98 =item $resp->header
99
100 Shortcut to $resp->headers->header
101
102 =item $resp->headers
103
104 Returns a L<HTTP::Headers> object containing the headers.
105
106     $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
107
108 =item $resp->output
109
110 Shortcut to $resp->body
111
112 =item $resp->redirect($url)
113
114 Contains a location to redirect to.
115
116     $c->response->redirect('http://slashdot.org');
117
118 =item status
119
120 Contains the HTTP status.
121
122     $c->response->status(404);
123
124 =back
125
126 =head1 AUTHOR
127
128 Sebastian Riedel, C<sri@cpan.org>
129 Marcus Ramberg, C<mramberg@cpan.org>
130
131 =head1 COPYRIGHT
132
133 This program is free software, you can redistribute it and/or modify 
134 it under the same terms as Perl itself.
135
136 =cut
137
138 1;