PDA

View Full Version : Perl Script runs different in my Linux...


Javier Mosqueda
07-02-2005, 01:07 AM
Hi again:

I have SuSE Linux 9.2 runing MySQL 4.0.21. and I've been working with MySQL for 2 years with no problems...until today. The case is one in which my perl script reads the query string and according to it, the script performs different tasks.

Task 1 deletes all records in a table and reads a textfile with tab separated records. Newlines are erased, every line is split to fill an array and an insert operation is perfmed on the table for every line in the textfile. That works in part, because the table is erased, loaded with the data in the textfile but there is a PRINT after every "insert" to the table that is not received by the browser WHILE the script runs in FQ, but it works as expected while the script runs in my SuSE.

Task 2 simply "selects all records" from the table and displays them in the browser. This works correct in FQ and my SuSE.


I tried 2 methods of inserting, one connecting and disconnecting to the mysql server for every record inserted. Other method is connect first, insert 334 records and disconnect, but both methods work almost the same, cause with first method I can telnet+mysql and select, I see all the timestamps of every records changed quite quickly (5 seconds or less). 334 records are erased and inserted inserted including one with empty key. After a delay of about 2 minutes, the record with empty key is removed and then the script finishes, but without printing those messages to the browser. Method b doesn't let me see (via telnet) more than few records until connection is over, then I see the 333 records. What I do see are the first and last timestamps:

20050701233604 and
20050701233806, or 0.36 seconds per insert, which I don't see it so bad, the weird thing again is the lack of printout.

The file processed is quite small, less than 70 KB when mysqldump-ed from the table with all the 334 records.

Following is that faulty Task 1

sqlhfastup/down is to connect/disconnect to/from the mysqlserver
sqlhfastrun performs do() with mysql statements.


if ($FORM{'rmp'} == 1) {
&sqlhfastup;
open MIARCHIVO, "$www/tablas/calcmat.csv";
@camporder=("clave","descripcion","grupo","especie","material","marca","um","moneda","precio","margenintegra", "preciosjr","precioalzado","notas","paridad","ppesos");
&sqlhfastrun("delete from calcmat");
while ($linix=<MIARCHIVO>) {
$linix =~ s/\n|\r//g;
undef @campix;
@campix=split /\t/, $linix;
$lx='';
for ($cc=0; $cc<=$#camporder; $cc++) {
if ($cc==0) {
print "<p class=\"forma\">$campix[$cc]</p>";
}
$lx.="$camporder[$cc]=\"$campix[$cc]\",";
}
chop $lx;
&sqlhfastrun("insert into calcmat set $lx");
}
close MIARCHIVO;
&sqlhfastrun("delete from calcmat where clave=\"\"");
print "<p class=\"forma\"><br>Ok!</p>";
&sqlhfastdown;
}

Thanks

(this is the script c t l o b r a located at x m y i n f i n i t y g l a s s)

kitchin
07-02-2005, 10:19 AM
Have you tried adding this line near the top of your script?

$|=1;

That turns off output buffering. It won't solve the 2 minute problem, but it might help with debugging, letting you see those print statements. Unless you already did that. :umm:

Javier Mosqueda
07-03-2005, 01:07 PM
Dear Kitchin:

I did what you suggested, RIGHT, the output is PARTIALLY printed (just first 30 records) and, yes, the delay is still very long, which sounds odd, because I run my SuSE on a CELERON 333 Mhz and the times are as follows:

First inserted record 20050703104727
Last inserted record 20050703104729

JUST 2 SECONDS DELAY for the 333 records

The times on the updated script uploaded to FQ are:
First 20050703115129
Last 20050703115331

Still 2 minutes for the fast FQ servers.?¿ :umm:

But must important, there are only those 2 timestamps along the records, this is, the first 20 records have the first time and ALL the remaining records have the last timestamp...

I guess there is a timeout caused by those 2 minutes which some times it is reported by my browser as "domain.com not found"....other times the browser finishes without change on the screen, the database is correctly updated but no printout is returned to the browser except those first 30 records now printed by the suggested $|=1

I don't consider there is a problem in the transmission over the internet, because the MySQL statements are performed locally within FQ, and SHOULD execute faster than in my modest CELERON 333.

Help.

Terra
07-03-2005, 02:53 PM
I created a new test directory: /cgi-bin/FQtest

copied in the script and stripped it down to the function you have shown above... This was done to isolate the routine for timing... Modifed the database to use '_6' for testing, instead of your live '_2'...

Running it from the command line gives the following timings:
$ time ./ctlobra.pl
(whole bunch of errors and warnings [mysql::db do failed] are printed here)
real 0m0.535s
user 0m.200s
sys 0m.010s

well under 1 second...

The 2 minutes you are seeing is happening somewhere else in your script...


sprinkle debugging PRINT statements before any major operations and at periodic locations
run the script via the command line via the debugger so that you can step through it
test to ensure that all data you are working with is valid, instead of making assumptions, especially to datum that you are shipping to the MySQL server... e.g. add robust error checking and handling
add '-T', '-w' and 'use strict;' at the top of your script, and fixup the script until it will run under 'use strict;' and 'taint mode'... This is one of the best Perl habits you can get into, **especially** when programming CGI scripts!


In cases like this, get it working from the command line first, before handing it over to Apache/CGI... Debugging by the command line is infinitely easier than trying to debug within the CGI environment...

--
Terra
--questioning everything leads to secure programming by default--
FutureQuest

Javier Mosqueda
07-03-2005, 03:08 PM
Thanks Terra:

I understand now but will be able to work on it tomorrow morning.

Greetings

Javier Mosqueda
07-04-2005, 05:57 PM
This is the second time a double quote character causes me serious problems.

I isolated the portion of the program with troubles, ran it via the shell there were all those messages reported by MySQL about some (rather many) syntax errors (those that are not sent to the web browser, but are sent to the shell).

The table had the inches symbol (") in many records and I was aware that it would cause problems remaining there, but I skipped it during the processing of each record. After the test via shell, I just =~ s/// the inches symbol by 2 single feet symbol (').

That solved what I though it was something way more complex... :smile:

Thanks Kitchin/Terra.