That's not the way the web works. Conceptually, web pages are served up whole. Most web browsers do actually render pages incrementally, showing the start of a page while the end is still downloading; but they don't do so consistently enough to be able to rely on this behaviour for your purposes.

That said, your desired end result can be achieved. It's just not simple, is likely to require a lot of rewriting for your script, and is probably more effort than it's worth. So you shouldn't bother.

Here's a general outline of how you'd do it. I'm not going to elaborate any further, and any requests for elaboration only go to prove that you'd ignored my overall advice not to bother!

First, split the script into two different parts:

Then create another CGI script, which when visited does the following:

  1. Invents a random temporary filename. (Data::UUID or similar is useful for this.)
  2. Spawns the Workhorse as a background process, asking it to log to the temporary file.
  3. Redirects to the Reporter, asking it to report on the temporary file.

Note I've consistently said filename, not file path. The filename should not include a directory portion. All the scripts should be hard-coded to read from a particular directory set aside for just this purpose. And your Reporter and Workhorse should validate that the filename they receive doesn't contain slashes or "..". This should prevent people using your Reporter to peek at other files on your system that you don't want them to have access to.

You probably also want to run a cron job on the server to mop up any temporary files older than, say, two hours. And if you know your Workhorse occasionally goes into an endless loop or otherwise spins off out of control, then you may also want a cron job to detect runaway horses and kill them.

If you were using a server environment with some persistence for Perl processes (e.g. mod_perl) instead of CGI, then this would be somewhat easier.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: 'print' to cgi like in terminal ("real-time") ? by tobyink
in thread 'print' to cgi like in terminal ("real-time") ? by iMisspell

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.