talexb has asked for the wisdom of the Perl Monks concerning the following question:
I received a query from a client about how to get autoflush working, so cobbled together a simple example. First, I did it in shell,
#!/bin/sh echo "Content-type: text/plain"; echo for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do echo "Value is $i"; sleep 1; done
This worked fine (it output a line every second), and I translated it into Perl.
#!/usr/bin/perl -w $|=1; use CGI qw/:standard/; { my $cgi = CGI->new; print $cgi->header('text/plain'); print "Autoflush is currently ".($|?"on":"off").".\n"; for ( 1..5 ) { print "Value is $_.\n"; sleep 1; } print "Autoflush is currently ".($|?"on":"off").".\n"; }
This didn't work. Nothing was output until the script finished. I was stumped. I search Perlmonks and found lots of information on autoflush questions, nodes Autoflush $|?, Autoflushing Laser printer, How to get explicity the autoflush var ($|) of a TIED HANDLER?, Autoflushing revisited., Simple autoflush module, OUTPUT_AUTOFLUSH, autoflush and 'DESTROY', autoflushing.. and When to use Autoflush. Nothing solved my problem.
Finally, over on IRC, I put my question and got a response from Caelum -- use PRE tags.
#!/usr/bin/perl -w $|=1; use CGI qw/:standard/; { my $cgi = CGI->new; print $cgi->header('text/html'); print pre("Autoflush is currently " . ($|?"on":"off") ); for ( 1..5 ) { print pre("Value is $_.</pre>"); sleep 1; } print pre("Autoflush is currently " . ($|?"on":"off") ); }
Well, to cut a long story short, it works. But why?
Shouldn't the autoflushing (happening down at layer 2 or 3) be immune to the presentation (up at layer 7)? Is this a Perl oddity?
What is going on?
Update: As fenLisesi points out, the closing pre tag in the 'Value is' print statement shouldn't be there, but I won't edit the original node to reflect that..
Update 2: I don't mind changing the title of this node to Autoflush and web browsers, but that's naming the thread after the fact -- after we know (or suspect) that it's the interaction with the browser that's causing the odd behaviour. In addition, it looks like most of the discussion on this thread is over (at the time of writing, the most recent reply to the root was shmem's dated 18 hours ago), so this consideration may not be necessary.
Update 3: Brother benizi makes a very good point -- retitling the node would be an aid to future searches, therefore it's a Good Thing. I've held off voting on the consideration or making a title change, but now I'm convinced.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Clever autoflush detail
by shmem (Chancellor) on Jun 20, 2007 at 21:12 UTC | |
Re: Clever autoflush detail
by BrowserUk (Patriarch) on Jun 20, 2007 at 18:46 UTC | |
by talexb (Chancellor) on Jun 20, 2007 at 18:51 UTC | |
by Joost (Canon) on Jun 20, 2007 at 19:28 UTC | |
by talexb (Chancellor) on Jun 20, 2007 at 19:45 UTC | |
by Joost (Canon) on Jun 20, 2007 at 19:56 UTC | |
Re: Clever autoflush detail
by Joost (Canon) on Jun 20, 2007 at 18:44 UTC | |
Re: Clever autoflush detail
by blahblah (Friar) on Jun 20, 2007 at 19:09 UTC | |
by Joost (Canon) on Jun 20, 2007 at 19:30 UTC | |
Re: Clever autoflush detail
by eric256 (Parson) on Jun 20, 2007 at 19:52 UTC | |
by talexb (Chancellor) on Jun 20, 2007 at 20:46 UTC | |
[OT] Re: Clever autoflush detail ... or ... Autoflush and web browsers
by benizi (Hermit) on Jun 25, 2007 at 02:56 UTC |