Gossamer Forum
Home : General : Perl Programming :

Print Output during cgi, not at end

Quote Reply
Print Output during cgi, not at end
I have found that depending on which browser I am using, the print statements in my perl subs won't dispay until the whole cgi has finished executing. This is a problem as I wish to update the user of the progress of the cgi as it runs.

Any ideas on how to overcome this without resorting to SSI, popups or javascript?

Example:

sub dosomething {

perl code here which does stuff for a while;
print progress update;
more lengthy perl code doing stuff;
print another update;
etc....

}


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Print Output during cgi, not at end In reply to
You need to use nph- (non parsed headers) and flush output using $| = 1;

There are issues regarding browsers that may cause it not to work though. If you search for nph you'll find one of my old threads about this.
Quote Reply
Re: [Ian] Print Output during cgi, not at end In reply to
Also, don't put your code that you want displayed real time inside tables. Some browsers won't display a table until all the html for the table has been received.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Print Output during cgi, not at end In reply to
Thanks guys.

I'll look up nph and avoid printing in tablesSmile


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Alex] Print Output during cgi, not at end In reply to
In Reply To:
Also, don't put your code that you want displayed real time inside tables. Some browsers won't display a table until all the html for the table has been received.

I learned this a long time ago....when trying to print out a 500 line table, and wondering why it wouldn't show til it all loaded Crazy

heh heh

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Alex] Print Output during cgi, not at end In reply to
Just wondering..I'm trying this on a test script, but it does't seem to be working. I'm adding this to the top of the sub;

# NPH stuff...
print "Content-type: text/plain\n\n";
$| = 1;

What else do I need to add? In links 2 etc, it use $nph++...is that a specific variable, or just used because you felt like it? Also, does the script HAVE to be called nph-script.cgi, or can I just call it script.cgi?

Thought I would have a play now that Ian brought the subject back up Tongue

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Print Output during cgi, not at end In reply to
Quote:
In links 2 etc, it use $nph++...is that a specific variable, or just used because you felt like it?

Mmmm, $nph is a global used for printing headers in db_utils.pl....

Code:
sub html_print_headers {
# --------------------------------------------------------
# Print out the headers if they haven't already been printed.
#
if (!$html_headers_printed) {
print "HTTP/1.0 200 OK\n" if ($db_iis or $nph);
print "Pragma: no-cache\n" if ($db_nocache);
print "Content-type: text/html\n\n";
$html_headers_printed = 1;
}
}
Quote Reply
Re: [Paul] Print Output during cgi, not at end In reply to
I'm still a little lost. I had a search on google, and that returned a few demo script....but once I try to make the script print out large amounts of data, it just does it the same as it I were not to use NPH :-/

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Print Output during cgi, not at end In reply to
Read the second paragraph in post 2.

Last edited by:

Paul: Oct 16, 2002, 5:51 AM
Quote Reply
Re: [Paul] Print Output during cgi, not at end In reply to
I take it you were refering to the header printed there. I now have;

# NPH stuff...
$| = 1;
#$|++;
print "HTTP/1.0 200 OK\n";
print "Content-type: text/html\n\n";

But that still doesn't do the load-as-you-go job Unimpressed

What am I doing wrong? I really wanna get a grasp on this feature, as I can see it coming in very helpful in the future Smile

BTW: I'm testing this in a plugin for LSQL, would that matter, due to the fact its not named nph-xxx.cgi ?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Print Output during cgi, not at end In reply to
Code:
$| = 1;
#$|++;

$| = 1 and $|++ do the same thing. $| should be 0 if it isn't 1.

server push is supposed to only work with Netscape/Mozilla, it won't work with IE, although I'm sure nph-build.cgi works with IE, oh well, according to O'Reilly it is only supported by Netscape.

Last edited by:

Paul: Oct 16, 2002, 6:18 AM
Quote Reply
Re: [Paul] Print Output during cgi, not at end In reply to
Just tried it in NS 6, and it still isn't working Unsure

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Print Output during cgi, not at end In reply to
What web server are you using? It could be the web server caching things? Also, if you are behind a proxy (i.e. a dual mod_perl setup), then the front end apache will cache the results before sending it to the user.

Finally, if you are on aol or cable, it might be your proxy caching the results for you. There are lots of levels of caching at work here. =)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Print Output during cgi, not at end In reply to
I can't get anything to work for me (using Apache and WinXP) and I'm using cable, yet nph-build.cgi works fine.
Quote Reply
Re: [Alex] Print Output during cgi, not at end In reply to
All I can think of is mod_perl, but like Pauls, nph-build.cgi works fine Unsure

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Print Output during cgi, not at end In reply to
Quote:
All I can think of is mod_perl,

You don't have mod_perl installed though do you?
Quote Reply
Re: [Paul] Print Output during cgi, not at end In reply to
Yup.

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Print Output during cgi, not at end In reply to
Not the proxied setup though.
Quote Reply
Re: [Paul] Print Output during cgi, not at end In reply to
I'm not sure on that. But, as I was saying, nph-build.cgi from LSQL/2 works fine on it...

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Print Output during cgi, not at end In reply to
I am using Chimera / Mozilla / IE Mac.... no problems for me. I use Apache (of course) and sometime firewall, sometimes not ;)

I will admit there are times when it does not seem to work, but as Alex said, caching might be the problem.


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
. In reply to
Not that I have tried it, but can't you use CGI to do this?

use CGI;

$| = 1;
my $q = new CGI;

print $q->header(-nph=>1);

or something like that, I read while figuring this out myself


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Oct 18, 2002, 8:50 AM
Quote Reply
Re: [Ian] Print Output during cgi, not at end In reply to
I realize I'm a year late, but I was googling for the answer to this question, and this thread came up ...

I could not get the $|=1 thing to work for me, so I attempted another solution, and it works. Here's couple of lines that'll show how it works:

use IO::Handle;
$io = new IO::Handle;
die "Ouch" unless ($io->fopen(fileno(STDOUT), "w"));
$io->print("<p>Print something</p>");
$io->flush();
$io->print("<p>Print somthing else</p>");