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.

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds


In reply to Autoflush and web browsers (was: Clever autoflush detail) by talexb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.