| 1 | # -*- Mode: perl -*- | ||
| 2 | # | ||
| 3 | # $Id: Buffered_Output.pm,v 0.1 2001/04/22 17:57:04 ram Exp $ | ||
| 4 | # | ||
| 5 | # Copyright (c) 1998-2001, Raphael Manfredi | ||
| 6 | # Copyright (c) 2000-2001, Christophe Dehaudt | ||
| 7 | # | ||
| 8 | # You may redistribute only under the terms of the Artistic License, | ||
| 9 | # as specified in the README file that comes with the distribution. | ||
| 10 | # | ||
| 11 | # HISTORY | ||
| 12 | # $Log: Buffered_Output.pm,v $ | ||
| 13 | # Revision 0.1 2001/04/22 17:57:04 ram | ||
| 14 | # Baseline for first Alpha release. | ||
| 15 | # | ||
| 16 | # $EndLog$ | ||
| 17 | # | ||
| 18 | |||
| 19 | use strict; | ||
| 20 | |||
| 21 | package CGI::MxScreen::Tie::Buffered_Output; | ||
| 22 | |||
| 23 | require Tie::Handle; | ||
| 24 | require CGI::MxScreen::Tie::Sinkable; | ||
| 25 | |||
| 26 | use vars qw(@ISA); | ||
| 27 | @ISA = qw(Tie::Handle CGI::MxScreen::Tie::Sinkable); | ||
| 28 | |||
| 29 | use Carp::Datum; | ||
| 30 | use Log::Agent; | ||
| 31 | use Symbol; | ||
| 32 | |||
| 33 | use constant HEADER => 0; | ||
| 34 | use constant BODY => 1; | ||
| 35 | use constant FILE_HANDLE => 2; | ||
| 36 | use constant WRITE_FIELD => 3; | ||
| 37 | |||
| 38 | # | ||
| 39 | # (TIEHANDLE) | ||
| 40 | # | ||
| 41 | # Initial tieing. | ||
| 42 | # | ||
| 43 | sub TIEHANDLE { | ||
| 44 | DFEATURE my $f_; | ||
| 45 | my $self = bless [], shift; | ||
| 46 | |||
| 47 | my $fh = gensym(); | ||
| 48 | open($fh, ">&STDOUT") || logdie "can't save STDOUT: $!"; | ||
| 49 | open(STDOUT, ">/dev/null") || logdie "can't reopen STDOUT: $!"; | ||
| 50 | |||
| 51 | $self->[HEADER] = ' ' x 10_000; # pre-extent | ||
| 52 | $self->[HEADER] = ''; | ||
| 53 | $self->[BODY] = ' ' x 100_000; # pre-extent | ||
| 54 | $self->[BODY] = ''; | ||
| 55 | $self->[FILE_HANDLE] = $fh; # saved STDOUT | ||
| 56 | $self->[WRITE_FIELD] = HEADER; # start to write into header | ||
| 57 | |||
| 58 | return DVAL $self; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub header { $_[0]->[HEADER] } | ||
| 62 | sub body { $_[0]->[BODY] } | ||
| 63 | sub fh { $_[0]->[FILE_HANDLE] } | ||
| 64 | |||
| 65 | # | ||
| 66 | # ->reset | ||
| 67 | # | ||
| 68 | # Reset state to "emptyness", clearing both BODY and HEADER and getting | ||
| 69 | # ready to get new data. | ||
| 70 | # | ||
| 71 | # Returns the length of BODY data we discarded. | ||
| 72 | # | ||
| 73 | sub reset { | ||
| 74 | DFEATURE my $f_; | ||
| 75 | my $self = shift; | ||
| 76 | my $discarded = length $self->[BODY]; | ||
| 77 | |||
| 78 | $self->[HEADER] = ''; | ||
| 79 | $self->[BODY] = ''; | ||
| 80 | $self->[WRITE_FIELD] = HEADER; # start to write into header | ||
| 81 | |||
| 82 | return DVAL $discarded; | ||
| 83 | } | ||
| 84 | |||
| 85 | # | ||
| 86 | # ->header_ok | ||
| 87 | # | ||
| 88 | # Headers has been written. | ||
| 89 | # Further output is buffered separately. | ||
| 90 | # | ||
| 91 | sub header_ok { | ||
| 92 | DFEATURE my $f_; | ||
| 93 | my $self = shift; | ||
| 94 | logcroak "called header_ok() more than once" | ||
| 95 | unless $self->[WRITE_FIELD] == HEADER; | ||
| 96 | $self->[WRITE_FIELD] = BODY; | ||
| 97 | return DVOID; | ||
| 98 | } | ||
| 99 | |||
| 100 | # | ||
| 101 | # ->discard_all -- redefined | ||
| 102 | # | ||
| 103 | # Discard all buffered data. | ||
| 104 | # | ||
| 105 | sub discard_all { | ||
| 106 | DFEATURE my $f_; | ||
| 107 | my $self = shift; | ||
| 108 | $self->[HEADER] = $self->[BODY] = ''; | ||
| 109 | return DVOID; | ||
| 110 | } | ||
| 111 | |||
| 112 | # | ||
| 113 | # ->print_all | ||
| 114 | # | ||
| 115 | # Print all buffered data sofar to the original STDOUT. | ||
| 116 | # The supplied $str is printed between HEADER and BODY. | ||
| 117 | # | ||
| 118 | sub print_all { | ||
| 119 | DFEATURE my $f_; | ||
| 120 | my $self = shift; | ||
| 121 | my $fh = $self->fh; | ||
| 122 | logcroak "$self already closed" unless defined fileno($fh); | ||
| 123 | local $\ = undef; | ||
| 124 | print $fh $self->[HEADER]; | ||
| 125 | print $fh $_[0]; | ||
| 126 | print $fh $self->[BODY]; | ||
| 127 | $self->discard_all; | ||
| 128 | return DVOID; | ||
| 129 | } | ||
| 130 | |||
| 131 | # | ||
| 132 | # (WRITE) | ||
| 133 | # | ||
| 134 | # Intercept writes | ||
| 135 | # | ||
| 136 | sub WRITE { | ||
| 137 | DFEATURE my $f_; | ||
| 138 | my $self = shift; | ||
| 139 | my ($buf, $len, $offset) = @_; | ||
| 140 | my $field = $self->[WRITE_FIELD]; | ||
| 141 | $self->[$field] .= substr($buf, $offset, $len); | ||
| 142 | return DVOID; | ||
| 143 | } | ||
| 144 | |||
| 145 | # | ||
| 146 | # (CLOSE) | ||
| 147 | # | ||
| 148 | # Restore orginal STDOUT, and flush buffers | ||
| 149 | # NB: unties STDOUT as a side effect. | ||
| 150 | # | ||
| 151 | sub CLOSE { | ||
| 152 | DFEATURE my $f_; | ||
| 153 | my $self = shift; | ||
| 154 | my $fh = $self->fh; | ||
| 155 | logdie "$self already closed" unless defined fileno($fh); | ||
| 156 | local $\ = undef; | ||
| 157 | print $fh $self->[HEADER]; | ||
| 158 | print $fh $self->[BODY]; | ||
| 159 | untie *STDOUT; | ||
| 160 | open(STDOUT, ">&=" . fileno($fh)) || logdie "can't restore STDOUT: $!"; | ||
| 161 | close $fh; | ||
| 162 | $self->[HEADER] = $self->[BODY] = ''; | ||
| 163 | return DVOID; | ||
| 164 | } | ||
| 165 | |||
| 166 | # | ||
| 167 | # (DESTROY) | ||
| 168 | # | ||
| 169 | # Destructor: ensure buffers are flushed if not already done | ||
| 170 | # | ||
| 171 | sub DESTROY { | ||
| 172 | DFEATURE my $f_; | ||
| 173 | my $self = shift; | ||
| 174 | my $fh = $self->fh; | ||
| 175 | return DVOID unless defined fileno($fh); | ||
| 176 | $self->CLOSE; | ||
| 177 | return DVOID; | ||
| 178 | } | ||
| 179 | |||
| 180 | 1; | ||
| 181 | |||
| 182 | =head1 NAME | ||
| 183 | |||
| 184 | CGI::MxScreen::Tie::Buffered_Output - Buferring of screen outputs | ||
| 185 | |||
| 186 | =head1 SYNOPSIS | ||
| 187 | |||
| 188 | # Not meant to be used directly | ||
| 189 | |||
| 190 | =head1 DESCRIPTION | ||
| 191 | |||
| 192 | This class is used to tie STDOUT from within C<CGI::MxScreen>, provided | ||
| 193 | the configuration variable C<$mx_buffer_stdout> is I<true>: see | ||
| 194 | L<CGI::MxScreen::Config>. | ||
| 195 | |||
| 196 | The advantages of buffering STDOUT are: | ||
| 197 | |||
| 198 | =over 4 | ||
| 199 | |||
| 200 | =item * | ||
| 201 | |||
| 202 | The context indication is emitted before any other screen output. This | ||
| 203 | prevents users from interacting with the form until everything has been | ||
| 204 | received, or at least until the context is there. | ||
| 205 | |||
| 206 | =item * | ||
| 207 | |||
| 208 | On fatal errors, we can properly discard the screen output and emit | ||
| 209 | the error page. | ||
| 210 | |||
| 211 | =item * | ||
| 212 | |||
| 213 | On screen bounces, we can properly discard any spurious output made | ||
| 214 | before the C<bounce()> call. See L<CGI::MxScreen::Screen>. | ||
| 215 | |||
| 216 | =item * | ||
| 217 | |||
| 218 | One day, we'll be able to automatically remove accentuated letters and | ||
| 219 | replace them with their entity escape sequence. | ||
| 220 | |||
| 221 | =back | ||
| 222 | |||
| 223 | The disadvantages are that there is a slight overhead due to tbe memory | ||
| 224 | buffering, and also that more memory is needed for the process to run. | ||
| 225 | |||
| 226 | =head1 AUTHOR | ||
| 227 | |||
| 228 | Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>> | ||
| 229 | |||
| 230 | =head1 SEE ALSO | ||
| 231 | |||
| 232 | CGI::MxScreen::Config(3). | ||
| 233 |