Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-19 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found