Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to follow the execution of my program but $|=1 seems to be not doing its job. It only shows the results when it finish to process. Could any help me. Here is the code I'm using.
#!/usr/bin/perl -wT BEGIN { $|=1; print "Content-type: text/html\n\n"; use CGI::Carp('fatalsToBrowser'); } use DBI; use CGI; $query = new CGI; $month = $query->param('m'); $year = $query->param('y'); $table_name = $month."_".$year; printf "Connecting to the database ............<br>\n"; ($dbhost, $dbname) = qw/xx.xxxxxxx.com.xx xxxxxxxxx/; ($user, $pass) = qw/xxxxxxxx xxxxxx /; $dbh = DBI->connect("DBI:mysql:hostname=$dbhost:database=$dbname", $us +er, $pass); printf "Creating temp table if not exist ............<br>\n"; $sql = "CREATE TABLE IF NOT EXISTS tmp_table (ClientIP Char(20),Qty IN +T)"; if (!$dbh->do($sql)) { printf "Error creating tmp_table table: " . $DBI::errstr; $dbh->disconnect; exit; } printf "Select from $table_name ............<br>\n"; $sql = "SELECT ClientIP, count(ClientIP) as Qty FROM " . $table_na +me; $sql .= " GROUP BY ClientIP"; $res = $dbh->prepare($sql); if ($res->execute) { while($data = $res->fetchrow_hashref) { $hits = int($data->{Qty}); if ($hits > 100) { printf "Insert " . $data->{ClientIP} . " - " . $data->{Qty} . " ...... +......<br>\n"; $sql = "INSERT INTO tmp_table (ClientIP,Qty) values (" +; $sql .= "'" . $data->{ClientIP} . "',"; $sql .= "'" . $data->{Qty} . "')"; if (!$dbh->do($sql) ) { printf "Error adding tmp_table table: $DBI::errstr +"; } } } } else { printf "Error selecting from " . $table_name ." table: $DBI:: +errstr\n"; } $res->finish; printf "that's all for now Folks!!<br>\n";

Edit by tye: title, READMORE

Replies are listed 'Best First'.
Re: flushing
by Gilimanjaro (Hermit) on Jan 23, 2003 at 01:04 UTC
    Your problem is probably that your web-server is caching (buffering) the results for you... Check the CGI man-page for info on NPH scripts.

    I know using CGI's NPH features got my script to work 'unbuffered' when using Apache.

    The 'nice' way to do it though would probably be to log your status to your apache error log (STDERR from a CGI perl), and keep track of your script through that channel. You should't debug in your browser window...

    Splitting your code into 'non-web-specific' parts that show optional debug info on STDERR also makes it more re-usable, so that you could easily convert your web-chore into for instance a daily cron-chore... Your CGI-script should merely be a wrapper around environment-independent modules/snippets/codelets or even nodelets should it come that far... :)

Re: flushing CGI output
by Anonymous Monk on Jan 24, 2003 at 13:17 UTC
    It would appear that you are telling the browser that you are going to be supplying a document of Content-type: text/html. Therefore, if you would like any data to appear in the browser you should print properly formated HTML.
    print <<ENDOFTEXT; Content-type: text/html\n\n <HTML> <HEAD> Some DB Data </HEAD> <BODY> ......... Database results here .......
    ENDOFTEXT Then don't forget to close the BODY and HTML tags at the end of the document.