[Tutor] Translating to Python [perl --> python]
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Mon, 1 Jul 2002 14:50:23 -0700 (PDT)
On Mon, 1 Jul 2002, Kyle Babich wrote:
> Could someone show me how to translate this to python from perl?
Hi Kyle,
Welcome! Let's see what a translation might look like between Perl and
Python.
> #!/usr/bin/perl -wT
> use strict;
Python is always in "strict" mode in a sense --- anything bad that happens
will raise an Exception. Python's magic line will probably look like:
#!/usr/bin/env python
> use CGI qw/ :standard /;
In Python, module importing is done through the 'import' keyword. Python
does have a 'cgi' module, so we can do:
###
import cgi
###
with similar effects.
See:
http://www.python.org/doc/lib/module-cgi.html
for more details on the 'cgi' module.
By the way, Python provides a really nice debugging module called 'cgitb'
that prints out a nice traceback if there are bugs in the module. Rather
than get a rude 'Internal Server Error', we can have our Python script
display a nicely formatted error page by doing:
###
import cgitb
cgitb.enable()
###
> print header ( 'text/html' );
I don't know if the Python cgi module has a header() utility function, but
we can do this for a similar effect:
###
print "Content-type: text/html\n\n"
###
> my $c = param('c');
> my $content = "c";
> if ($c eq "abc") {
> $content = qq{abc123};
> } elsif ($c eq "def") {
> $content = qq{def456};
> } else {
> print "error: content failed\n";
> }
Python's cgi module provides an object that we use to pull out parameters.
Here's what it might look like:
###
form = cgi.FieldStorage()
c = cgi['c'].value
content = 'c'
if c == 'abc':
content = "abc123"
elif c == 'def':
content = "def456"
else:
print "error: content failed\n" ## shouldn't we raise an error here?
###
> open(TEXT,"text.txt") or die ("error: text.txt failed\n");
> my $text = <TEXT>;
> close(TEXT) or die("error: close text.txt failed\n");
File opening in Python implicitly raises an IOError if bad things happen,
so the "die" part is implied.
###
file = open("text.txt")
text = file.read()
file.close()
###
> print <<"EndOfHTML";
>
> $content
>
> <br> <br> <br>
>
> $text
>
> EndOfHTML
Python provides a string formatting operation that can span across several
lines:
###
print """
%(content)s
<br> <br> <br>
%(text)s
""" % vars()
###
Let's put everything together to see what the final product looks like.
I'll do a little rearrangement to make the program flow a little nicer:
###
# Translation of Kyle's Perl program into Python
import cgi
import cgitb
cgitb.enable()
form = cgi.FieldStorage()
c = cgi['c'].value
content = 'c'
if c == 'abc':
content = "abc123"
elif c == 'def':
content = "def456"
else:
print "error: content failed\n"
file = open("text.txt")
text = file.read()
file.close()
print "Content-type: text/html\n\n"
print """
%(content)s
<br> <br> <br>
%(text)s
""" % vars()
###
This is still a little awkward as far as Python programs go --- I'd like
to break things down into separate functions to make the code's intent
clearer, and to avoid having global variables spill out everywhere.
Hope this helps!