The Writing Confederation
Computers, anime, and writing – A confederation of topics
An Introduction to Python and Perl, and Running them in Windows
Posted by on June 23, 2011
In my last article, I went through how to install the tools needed to create, edit, debug, and run Perl and Python, specifically CGI files, in Windows. As a brief recap of Running Perl and CGI files on Windows, here’s what I went over:
- Download and install XAMPP. XAMPP gives you the ability to run Perl and PHP scripts natively in your browser as if they were on a web server.
- Download and install Python. By installing Python, and combining it with XAMPP, you can not only create CGI files, but also run them on your computer.
- Download and install Cygwin. Cygwin offers some of the tools available to those that have access to a Unix terminal, as well as provides the ability to create, edit, debug, and run virtually every type of program.
By downloading and installing those three programs and following my guide, you can attain the ability to easily create, run, edit, and debug programs of any kind. But what if you don’t know how to write, say, a CGI file to make a webpage? What if you’re not an expert, or even a beginner, in Perl? If you aren’t a programmer yet, then this guide is for you: in the rest of this post, I will go over what you need to know to be able to create web-ready CGI and Perl files, as well as some basics of each language.
The first thing I will cover is creating the basic template for a webpage in both Python and Perl. Since the main focus of this article is to give you the knowledge to create a webpage with Python and Perl, that is what I will focus on most; basics of both language, while you will learn them throughout this article, will not be focused on very much.
Alright then, the first template we’re going to go over is a webpage created with Python. In order for a Python file to be opened in a browser and displayed as a webpage, it must be saved with the .cgi file extension, and therefore I will refer to webpages created in Python as CGI files from here on out.
Anyone that has created a Python program in the past will know that the shebang statement (pronounced she-bang statement) is the first line of any Python file. Normally, it looks like this:
1: #!/usr/bin/python
The shebang statement tells the computer that this file is, in fact, a Python file, and that the computer should look for Python in the folder /usr/bin/python. /usr/bin/python is the default Python installation folder on Unix systems. But you aren’t running Unix, are you? At least, as far as I am concerned in this tutorial, you are running Windows, and if you are running Windows you’re going to need to change that shebang statement a bit. If you are running a Unix-based system, the shebang statement above will work. The new shebang statement that will work on Windows, assuming you installed Python 2.7 and installed it to C:/Python27, will look like this:
1: #!C:\Python27\python.exe
If your version of Python is not 2.7, replace “27” with your version number, but strip off the periods. For example, if your version was 3.1.4, your Python installation folder would probably be named Python314, although since I am not sure of this, check by opening up your C: drive and checking the folder name.
The shebang statement for a Perl file is pretty much the same as the shebang statement for Python on Unix systems:
1: #!/usr/bin/perl
However, to be able to run the Perl files in Windows with XAMPP, you must change the shebang statement to look like this:
1: #!C:\xampp\perl\bin\perl.exe
In each case, whether with a CGI file or a Perl file, the shebang statement needs to point to the location of python.exe or perl.exe, respectively.
Alright then, now that you have the shebang statement down, let’s move on to making a basic webpage with Python and Perl. For both Perl and CGI files, the process for creating a file that will cause (X)HTML code to be displayed to the screen is very much the same, so I will go through both at the same time.
In order to create a displayable webpage either in Perl or Python, you will need your file to look like, at the bare minimum, this:
1: #!C:\Python27\python.exe
2:
3: print "content-type:text/html"
4: print """\
5: <html>
6: <head>
7: <title>
8: Page Title
9: </title>
10: </head>
11: <body>
12: Page content
13: </body>
14: </html>
15: """
Obviously, “Page Title” and “Page Content” would be changed to whatever you wanted them to be changed to, and the shebang statement would be changed if you were creating a Perl file. This is the basic file for creating a webpage that will be displayed to the screen from either a CGI file or Perl file, depending on the shebang statement you include. Before moving on to test out this file though, let’s run through a little bit about this program. If you are new to programming, pay attention to this. Otherwise, feel free to skip over this paragraph.
- Line 1 is, as you know, the shebang statement.
- Line 2 is left blank to make the file a bit easier to read. Remember that the computer ignores whitespace like this, so you could have anywhere between one and 100 lines after the shebang statement and it would not matter. This rule is applicable for almost any part of a program, and is a great way to make the program more readable.
- Line 3 is critical to this program: it tells the browser that the, in this case, CGI file is displaying text and HTML code, and to treat it as such. If you do not include this line, an error will be thrown when you try to open this file in the browser.
- Line 4 begins the XHTML code. In Perl and CGI files, (X)HTML code is handed to the browser via the printstatement. Something you might not know about, though, is the rest of line 4: notice the three double-quotes, followed by a backslash ( \ ). Three double-quotes followed by a backslash tells the program that 1) to continue the contents of the print statement until it encounters another set of three double-quotes, and 2) that the text, or in this case the code, to be printed begins on the next line.
- Lines 5-14 contain the XHTML code that is to be printed to the browser, finishing on line 15 with the three double-quotes that denote the end of the print statement.
Depending on the type of file you are creating, whether a CGI file or a Perl file, save this file as index.cgi or index.pl, respectively. As I said in my last article, you will need to save this file in the cgi-bin directory, which is within the XAMPP root directory. The default XAMPP root directory is C:/xampp, and by default the cgi-bin folder will be located at C:/xampp/cgi-bin. Save the file in the cgi-bin directory, otherwise it will not execute when opened in the browser. Once the file is saved to the cgi-bin directory, open up your browser but don’t navigate to any any webpage or open a file.
From your browser, you can go to http://localhost/ in order to make sure that XAMPP installed successfully. If you see a webpage come up and not an error message, XAMPP was installed correctly. Note that instead of being at http://localhost/ or localhost/, you were redirected to localhost/xampp (or http://localhost/xampp). In order to be able to view the CGI or Perl file you created just a minute ago, go to http://localhost/cgi-bin/index.cgi, or http://localhost/cgi-bin/index.pl for a Perl file, where index.cgi (or index.pl) can be replaced with the file name of any file you wish to execute. If you received an error, don’t worry about it—”It’s all… part of the plan.”
So you got an error, no big deal—if you plan to program regularly, or even just every once in awhile, get used to having errors. In my previous article I suggested that you install Cygwin in order to be able to more easily debug programs. Assuming that you installed Cygwin, fire it up. If you did not, I recommend that you do so now.
Once Cygwin, or rxvt if that is your preferred editor, loads, you’re going to need to navigate to the cgi-bin folder. If you read my post, Installing Cygwin, you should know how to do this. If you need a bit of help with it though, the command will look like this assuming you installed XAMPP in the default directory:
cd "C:/xampp/cgi-bin"
Unless you installed XAMPP in a different folder other than the default, which is C:/xampp, this will take you directly to the cgi-bin folder. Once there, you are going to want to do a one thing before everything else: change the file permissions of your file. This is just good practice and can save you some time later on. It could also be the source of your problems on occasion, if the browser does not have permission to execute the file, for example. For brevities sake, I will assume that you created a CGI file and named it index.cgi. In order to change the file permissions of index.cgi, use the following command:
chmod 755 index.cgi
This command changes the permissions of index.cgi to 755, which means that everyone can read and execute the file, and the owner of the file has write permissions—he or she can make changes to the file and save it. Alternatively, you can also use this command to change the permissions of all the files in the current directory:
chmod 755 *
For more information on permissions and setting them with Cygwin, check out UNIX Permissions Help from zzee.com.
Next, let’s take a look at the file by using the cat command. The cat command prints the contents of the file it is supplied with to the standard output, which is the screen (or the terminal window).
cat index.cgi
You should now see the contents of index.cgi printed to the screen. Everything look good? Unless you notice any obvious errors in the syntax of your program, proceed to the next step. If you do notice something wrong, make the change to your file, save it, and try to open it again in the browser. It should fail. Once you’ve finished testing and reviewing it, proceed to the next step. If it works, though, I still recommend that you continue reading the next section so that you will have some knowledge on how to debug one of the most common issues of creating programs on Windows.
Now that you are thoroughly confused, I will tell you what is wrong with your program: most text editors for Windows add a little something onto the end of each line that is incompatible with Unix systems and shows up in files read by them. That something is a ^n character and must be removed in order for your files to be successfully run. If you want to see them, use the following command:
cat -v index.cgi
The cat –v command will show all the hidden formatting that is otherwise invisible in your program. To remove those annoying ^n’s from your program, run this command in Cygwin:
dos2unix index.cgi
Just like with the chmod command, using a splat (star) instead of the file name will cause the command to be applied to all files in the current directory. To check to see if all those ^n’s were removed from your program, run the cat –v command again. You should’ve see any. Now open your browser once again and refresh the page—voila, it should work just fine. Don’t worry about having to do this every time though: once you’ve used the dos2unix command on a file, the extra formatting characters tend to stay off your file regardless of the editor you use, so long as it isn’t Microsoft Word or something similar.
Your screen should be, for the most part, empty right now except for two words: “Page Content”. Those two words are exactly what you told the CGI file to print to the screen. You may be wondering what happened to all the XHTML code though: it was read by the browser and then parsed as XHTML code. To make this a bit easier to understand, think of it this way: the CGI file is executed by the browser when you open it. The CGI file is opened by navigating to it in the browser. Upon being executed, the CGI file sort of hands the browser everything it is told to have printed. In this case, you can think of everything printed to the screen except the first print statement, the one proclaiming the content of the document as text and HTML code, as an XHTML file. When thought of this way, it makes sense that the only text printed to the screen is the single line within the <body></body> tag: Page Content.
Now that you’re thinking of the code within the print statements as a sort of HTML file, here’s your first tip: write your basic webpage in an HTML editor, then copy and paste it between the print statement. Doing this will make editing and creating the basic webpage much, much easier since HTML files can be quickly run and then debugged using tools like Firebug and Web Developer Toolbar, whereas with a CGI file you will also need to debug the Python code and not only the code rendering the webpage. Supposing you take my advice and decide to edit your (X)HTML code in an HTML editor, what editor will you use? There are a host of different editors available, but my favorite is Notepad++. In Running Perl and CGI files on Windows I recommended that you download and install Notepad++ to create and edit programs, and I will recommend it once again now: Notepad++ not only is a nice editor for Python and Perl programs, but also works well with (X)HTML code as well. To download Notepad++, visit their website here: Notepad++ home page.
So now you’ve got the basic code for a webpage working, you have a basic webpage, and an editor to edit both the CGI file and the HTML code. On top of that, you know that simply copying and then pasting the HTML code into the CGI file between the first three double-quotes and the last three double-quotes will cause that HTML code to be read by the browser as if it was an HTML file, and therefore you can easily edit and create basic webpages separately from your CGI file, then stick it in there when you’ve finished. Now that you know all this, what’s next? Let’s go over some basics of Python and Perl. First, I will go over some Python basics:
Declaring variables in Python
This is such an easy thing to do, it hardly warrants its own section. Nevertheless, it is a necessary topic and will therefore be covered, even if briefly. Programmers coming over from languages like C where you must define variables by type, and once a variable is defined as, say, an integer it cannot be a string or even a float later on may find the manner in which Python uses variables strange. Rather than explain this, check out the code example below, as well as the screenshot showing the results:
1: #!C:/Python27/python.exe
2:
3: hello = "hey"
4: print hello
5: print
6:
7: hello = 1
8: print hello
9: print
10:
11: hello = ["hello", "hey", "hiya"]
12: print hello
13: print hello[0]
14: print hello[1]
15: print hello[2]
16: print
17:
18: hello = {"first" : "hello", "second" : "hey", "third" : "hiya"}
19: print hello
20: print hello["first"]
21: print hello["second"]
22: print hello["third"]
23: print
And the result:
So here, you can see that the first time I define the variable hello I fill it with the text “hey”. Using the print command, I can print the contents of the variable hello, resulting in the text “hey” being printed to the screen. Next, I using a single print command. This causes a blank line to be printed to the screen, giving some space before the next print statement.
The next time the variable hello is defined, it is defined as an integer. Unlike in a programming language like C, re-defining a variable like this, moving from one type to another, is perfectly acceptable. This time, hello is given the contents of the number 1, and using the print statement will print a 1 to the screen. In case you are wondering, the 1 is actually an integer even though the variable hello was previously a string variable. That is, I could add a 1 to hello and get 2, not 11 as it would be if I added “1” and “1” (both being strings).
The second time hello is redefined, it is defined as something called a tuple. A tuple is a number of values separated by commas and accessed by using the index; that is, the item at the 0th index is actually the first item in the tuple since tuples begin counting at 0, the item at the 1st index is actually the second item in the tuple, and so on. Here we have re-defined the variable hello once again, and once again have changed its date type.
The fourth and final re-definition of hello defines it as something called a dictionary. A dictionary can be thought of sort of like a tuple, except instead of using numbers as the index, a dictionary uses a string as the index for each of the elements. If you look back up at the code example, different items in the tuple are accessed by using the indexs 0, 1, and 2, whereas the different items in the dictionary are accessed by using the strings assigned to each element:
dict = {"item one index" : "contents of item 1", "item two index" : "contents of item 2"}
Declaring variables in Perl
Rather than do all the basics of Python at once, I’m going to do each topic of each language together. Therefore, this section will be devoted to explaining variable definition in Perl. Perl variables, like Python variables, determine their type from the contents given to them, which means that a variable can be an integer for a while in a Perl program, then a string, then a list—just like in Python. Here’s a code example, as well as a screenshot of the result:
1: #!C:/xampp/perl/bin/perl.exe -w
2:
3: $myvar = "hey";
4: print $myvar . "\n\n";
5:
6: my $myvar = 1;
7: print $myvar . "\n\n";
8:
9: @myvar = ("hello", "hey", "hiya");
10: print @myvar . "\n";
11: print $myvar[0] . "\n";
12: print $myvar[1] . "\n";
13: print $myvar[2] . "\n\n";
14:
15: %myvar = (mon => "Monday", tue => "Tuesday", wed => "Wednesday", thur => "Thursday");
16: print %myvar . "\n";
17: print $myvar{"mon"} . "\n";
18: print $myvar{"tue"} . "\n";
19: print $myvar{"wed"} . "\n";
20: print $myvar{"thur"} . "\n\n";
And the result:
Alright then, let’s talk about this example: the first line is, as I’m sure you know, the shebang statement. Because I wrote this example in Ubuntu, the shebang statement was as you see it in the example rather than how I told you to write it on Windows. The first line is followed by some whitespace, which is good programming practice and makes the file a bit easier to read.
Line 3 begins the variable definitions. First, the variable myvar is defined as a string with the contents of “hey”. Notice the dollar sign ( $ ) before myvar: this declares the following variable, in this case myvar, as something called a scalar. A scalar is a data type that is like a normal variable and can contain any sort of data—any sort of number or string, or both, but not an array (a tuple in Python) or a hash (a dictionary in Python). The declaration of myvar is followed by a print statement, which prints the contents of the scalar myvar to the screen. Notice the space after the name of the variable to be printed, followed by a period ( . ), with “\n\n” following the period. By default, Perl does not print each print statement on a new line (whereas Python does), so if you wish to print each print statement on a new line, you must include a period after the variable name—the period is the Perl concatenation operator—and then the newline character, which is \n.
The next time myvar has its value changed doesn’t actually change its data type: it is still a scalar. However, the value of myvar is changed from a string to an integer. myvar is then printed to the screen in the same manner as it was printed to the screen when it had a string value, and is followed by two newline characters concatenated to the print statement with the period as with the previous print statement.
Next, myvar finally has its data type changed: myvar goes from a scalar to something called an array. An array in Perl is almost exactly the same as a tuple in Python, and is accessed in the same manner through indices. Check the code example for syntax help.
On line 15, myvar is once again re-defined, and this time as something called a hash. A hash in Perl is very similar to a dictionary in Python, and is accessed almost the same way: by using a string as an index. Check the code example for syntax help.
So that’s defining variables in Python and Perl. Defining variables in both language is pretty easy, but I still suggest you take some time to memorize the syntax of each: whether you need curly braces or square brackets to define a tuple in Python, or if you need to use the @ sign before a list or the percent sign ( % ) before a hash in Perl, for example.
Variable substitution in Python
This is pretty easy, but extremely powerful and unbelievably useful, and especially so when creating webpages in Python. When I say variable substitution, I mean substituting for example, the value of a variable into a webpage. For example, take a look at the code example below, as well as the output screenshot:
1: #!/C:/Python27/python.exe
2:
3: #DEFINE THE VARIABLES
4: page = "Home"
5: content = "Home page!"
6: #END DEFINE VARIABLES
7:
8: #PRINT THE PAGE
9: print "content-type:text/html"
10: print """\
11: <html>
12: <head>
13: <title>
14: %s
15: </title>
16: </head>
17: <body>
18: %s
19: </body>
20: </html>
21: """ % (page, content)
22: #FINISHED PRINTING THE PAGE
Before going to the output, what do you think will happen? Notice the %s operators in the code example: There are two, one between the opening and closing <title> tags, and one between the opening and closing <body> tags. Also notice that I have defined two sting variables at the beginning of the program; one named page, and the other named content. You can read the content of both of these variables by checking out the program. Finally, notice that at the end of the program, rather than simply having the three double-quotes denoting the end of the XHTML document, there are a few extra things on the end of the line: the percent sign ( % ) followed by the name of both variables in parenthesis. When this code is run, here’s the output:
Notice that both %s’s were replaced with the contents of the variable page and the contents of the variable content, respectively. Here’s why: the %s operator means to substitute a variable’s contents in at that point. The variable whose content is to be placed in the print statement is decided upon by the order of the variables listed at the end of the print statement, within the parenthesis: the first %s in the program is replaced by the first variable inside the parenthesis, the second %s in the program is replaced by the second variable inside the parenthesis, and so on.
Variable substitution is extremely useful in general Python programming, but especially useful when creating webpages. For example, with variable substitution it is possible to have a basic page layout and change only certain things, such as the page title and the body content, by changing the required variable’s contents. Take a look at this code snippet for a better example of the power of variable substitution:
1: #!C:/Python27/python.exe
2:
3: #DEFINE VARIABLES
4: page = "Home"
5:
6: content = """\
7: This is a multi-line text example.
8: Somehting like a menu could be placed here, and then substituted into the program later on.
9: A menu created in this manner would be much easier to manage because it could be changed in a single place rather than on every page.
10: So that's pretty much it!
11: """
12:
13: #END DEFINE VARIABLES
14:
15: #PRINT PAGE CONTENT
16: print "content-type:text/html"
17: print """\
18: <html>
19: <head>
20: <title>
21: %s
22: </title>
23: <head>
24: <body>
25: %s
26: </body>
27: </html>
28: """ % (page, content)
29: #END PRINT PAGE CONTENT
The output would look like this:
This example demonstrates the power of variable substitution in Python, and why it is so useful creating webpages. I recommend that you try this out a couple times, and just play around with some different variable substitution programs of your own creation in order to get better at this. You’ll be happy you did later on.
Variable substitution in Perl
Unlike Python, Perl does not have a easy-to-use operator to substitute in the contents of variables. If I am incorrect here, which is very possible as I have used Perl very little, please comment and correct me. Anyways, the fact of the matter is that you must manually insert variables one at a time, rather than all at once like Python does. Here’s a code example that would produce the same results as the above output screenshot generated after running the Python example code:
1: #!/usr/bin/perl -w
2:
3: $content = <<BODY_CONTENT;
4: This is a multi-line text example.
5: Somehting like a menu could be placed here, and then substituted into the program later on.
6: A menu created in this manner would be much easier to manage because it could be changed in a single place rather than on every page.
7: So that's pretty much it!
8: BODY_CONTENT
9: $page = "Home\n";
10:
11: print "content-type:text/html";
12: print <<HTML;
13: <html>
14: <head>
15: <title>
16: $page
17: </title>
18: <head>
19: <body>
20: $content
21: </body>
22: </html>
23: HTML
This code definitely needs some explaining! I’ll go through it line-by-line:
1. You’re probably tired of me saying this by now, but I’ll say it again anyway: the first line here is, as always, the shebang statement.
2. The second line is, again, as usual, a blank space to give us a bit of room between the beginning of the file and the actual good. As I have said before, this is simply good programming practice.
3 – 8. Line three defines a variable named content as a scalar—that’s easy enough to see. However, what’s that weird <<BODY_CONTENT; line after the variable definition? <<BODY_CONTENT; is something called a here-doc. A here-doc (which is in this case BODY_CONTENT) allows for multi-line strings without worrying about quotation marks and the like. It is very similar to Python’s usage of three double-quotes ( “”" ) and works exactly the same way. In order to end a here-doc, you must use the same words that were used in the initialization of the here-doc; that is, if you initialized a here-doc with the words BODY_CONTENT, then the here-doc will not end until you type the words BODY_CONTENT. The two less-than symbols ( << ) merely declare that the following word (or words connected by an underscore) define a here-doc. To help solidify the here-doc, here’s a quick example:
#!C:/xampp/perl/bin/perlexe -w$multi_line_string = <<MANY_LINES; This is part of the scalar variable multie_line_string, As is this, And this, And this, too. This is the end: MANY_LINES
You can also go to About.com’s here-doc page for more information on the here-doc: Perl Here-Doc Tutorial.
9. The ninth line in the example code above defines a scalar variable named page and gives it the value “Home”. This variable is used to specify which page is currently open, and as you will see later is incorporated into the XHTML code.
10. Line 10 is another empty line to separate the variable definitions from the code that actually prints the XHTML markup to the browser. Again, simply good programming practice.
12-22. Line 12 begins the printing of the XHTML code. Notice that instead of a regular print statement, this print statement uses a here-doc so that the XHTML code can be typed out as it would appear in the browser; that is to say, on multiple lines without the use of multiple print statements. This here-doc print statement spans lines 13 through 22 and ends with the name of the here-doc: HTML.
Line 16 and Line 20. Instead of using the %s operator like we did in Python, Perl requires that the variable name by typed out in the code in order for it to be displayed. Therefore, on line 16 the page variable is included in the print statement that prints the XHTML code to the screen, and on line 20 the content variable is included in the print statement which prints the XHTML code to the screen. If you would like to print “$page” instead of the contents of a scalar variable named page, you would simply add a backslash before the dollar-sign, so it would instead look like this: \$page.
Quick tip: the backslash “escapes” the character it precedes. For example, if you use the line of code below in a Perl program, you will end up printing “Hello, world!” to the screen:
$myvar = "Hello, world!";
print $myvar;
However, if you were to use the code below in your program, you would end up printing “$myvar” to the screen—the actual text “$myvar”, not what is inside the scalar variable named myvar.
$myvar = "Hello, world!"; print "\$myvar"
The same goes for Python: if you want to escape a character, such as a double-quote inside a string, use the backslash:
hello = "Hello, world! I'm \"Bob\"."
print hello
This would cause this line to be printed to standard output—the screen: Hello, world! I’m “Bob”. If the two backslashes were omitted, an error would be thrown when the Python program was run since there would be incorrect formatting.
That concludes my basic introduction to Python and Perl. Since I went through all the steps to give you the ability to create, edit, debug, and run Python and Perl programs (as well as CGI files) in my last post, Running Perl and CGI files in Windows, I will not go through them in too much depth again. However, I will go through them briefly. For a more detailed guide and explanation, check out my last post.
In my last post I recommended that you install the following programs:
- XAMPP. XAMPP allows you to run Perl, Python (CGI), and PHP scripts as if they were hosted on a server. Without this ability, you can only run your webpages created with Perl and Python and see the resulting (X)HTML code—not very helpful when designing a website. Additionally, XAMPP also automatically installs Perl, so you don’t have to do that separately as you have to do Python.
- Python. If you wish to create CGI files, you must have Python installed. XAMPP does not come with an option to install Python, so unless you install Python manually you will not be able to run CGI files through XAMPP.
- Cygwin. This is probably the most important programming tool for anyone what is planning on programming very much. Cygwin allows you to run and debug Perl, Python, C, and many other kinds of programs right from the terminal. Additionally, Cygwin also gives you the ability to use Unix terminal commands, which makes a lot of programming tasks much easier.
- Notepad++. Notepad++ is my favorite free editor for programming. Notepad++ features syntax highlighting, which is extremely helpful when programming, as well as a host of other features that just make programming much easier.
Once you have these four programs installed, you can create, run, debug, and deploy programs written in Perl, C, Python, and even Shell—just to name a few, and all from Windows.
Comments are closed.