Perl to Python
Tim Hammerquist
tim at vegeta.ath.cx
Mon Nov 26 23:29:38 EST 2001
Paul Malinowski <paul.malinowski at wulfsberg.com> graced us by uttering:
> I am relatively new to programming and very new to Python.
> I have a number of Perl/CGI scripts on a Linux/Apache web
> server that run queries to
> MS Access databases through an Openlink service running on
> an NT server. Works great! I have been trying to do the
> same thing with Python and am not having much luck. Would
> it be to much to ask one of you Python guru's to help me
> translate this from Perl to Python? (I intentionally left
> out the paths in the Perl environment variables to the
> OpenLink installed client on my Linux system to hide user
> names and such...)
>
> Any help would be really appreciated!
I gave some Python tips in another thread, but I feel obligated to point
out a problem in the Perl:
[ snip ]
> if (!open(SQL,"$sql |")) {
> print "Unable to open connect to database<br>";
> exit 0;
> }
>
> if (open(SQL, "$sql |")) {
> chomp(@rawdata = <SQL>);
> }
Another poster was correct. You're opening a pipe from $sql _twice_.
Why?! Connecting to a database is an expensive process, and you're not
even protecting yourself against the event you're (apparently) trying to
avoid. Try:
unless (open SQL, "$sql |") {
# you might also want to output _why_ it failed
print "Unable to connect to database<br>";
exit 0;
}
else {
chomp(@rawdata = <SQL>);
}
And as any perler would tell you, you should be running with 'use
strict;' unless you have a specific reason not to. Perl's
autovivification does not inherently have the safeguards that Python
does.
Tim Hammerquist
--
We are Pentium of Borg. Division is futile. You will be approximated.
-- seen in someone's .signature
More information about the Python-list
mailing list