Join Bill Weinman for an in-depth discussion in this video anatomy of a Perl script, part of Using Perl/CGI Scripts.
- In this lesson, you're going to learn a little bit about the anatomy of a Perl script and how to edit it. And then, upload it and see it work. Make a little change and send it up to your server and see it work on your server. So, let's open the test.cgi program in your favorite text editor. And let's just take a moment and talk about the text editor. This is a little bit more important now than it was in the case of your HTML files because the server can be very, very picky about the actual format of the file when it's trying to run it as a Perl script.
Now, I use a text editor called TextPad, which is available at textpad.com. I'm not affiliated with them. For Windows-based editors, it's the one that I've found that I like the best. The reason for that is that it's flexible. It's got a lot of features, and it tends to get out of my way enough. I like that. The thing to keep in mind about an editor is that, 1) It's a plain text editor that it's not a word processor. You can't use a HTML generator for this.
You can not use Word or a word processor for this because all of those things are going to add stuff to the file that will make it not work. A Perl program or any program is a very very specific thing, and it needs to be just the text and only the text. And even that has to be in a particular format. Now, we need to talk briefly a little bit about line endings. There's two different schools of thought about how to transfer files with line endings.
But first, we need to understand what they are and why they're an issue. Different operating systems tend to prefer different types of line endings. The line ending is the code that the computer inserts at the end of a line... so that it can understand where one line ends and the next begins. Now, from the human perspective, one line ends and the next begins at the end of the line and at the beginning of the line. But, for a computer, it doesn't have a visual representation. It only really has a stream of bits.
And so, it needs to understand what exactly is the end of a line. And if you tell it that one thing is the end of the line, and it finds something else, it's just not going to know that the line has ended. So, unfortunately for those of us that aren't computers, there are three different standards for how line endings are represented in the computer code. And those three different standards are all very prolific. UNIX-based systems consider a line ending a bare line feed character.
DOS or Windows-based systems consider a line ending a carriage return followed by a line feed character. And Macintosh-based systems consider a line ending a carriage return by itself. And those are three different things. And unfortunately, the computers can't always tell one from the other at the level they're reading a Perl script. So, the good news is is that Perl... on all servers tends to understand UNIXish line endings.
So, even if you're using a server that's running on Windows NT or that's running on a Macintosh, the UNIXish line endings are almost certainly going to work. And I wish I could say 100%, but I can't. So, the best thing for you to do is to have the line endings be whatever the native line endings are on the servers. So, that mean know your server. Now, it used to be that you could just edit it in a text editor and use the native line endings that work on your operating system.
And then, send it using an FTP program in text mode. And if you're using regular FTP, that will work at least most of the time. I wish I could say all of the time, but unfortunately the way that that works, leaves some to be desired. And so, because of that leaves some to be desired part, what I prefer to do is to edit the file and save it on my computer in the format that the server wants it. And then, go ahead and transfer it as a raw binary file.
And that always works 100% of the time. The other reason that the send it in text or asking mode doesn't work is because many new servers are using a different protocol than FTP. It's called SFTP, secure FTP. And unfortunately or fortunately, however you look at it, SFTP doesn't have that part of the protocol that does the translating of the line endings. Now, because it never worked 100% of the time anyway, I consider that a good thing, but not everybody does, and I can understand that.
So, my recommendation, and you can take it or not, is to Configure your text editor to always use the types of line endings that your server uses. Now, in this case, I have set it to UNIX line endings. And in my text editor, this is where it's configured, it's gonna be someplace else in yours almost certainly if you're not using this one. But, I set it to Create New Files as UNIX.
It gives me this choice of PC, UNIX, or Mac, and what it means is the line endings. And that's the way, I just do it. All of my servers run UNIX. A lot of other servers out there run UNIX. If your server is running Windows NT, you might want to set it to save the files as PC. If you set them to save them as UNIX, they're still gonna work because it's Perl, and Perl is pretty good about that. But, find out what the native line endings are on your server and save your files that way and send them in binary mode.
I think that's the right way to do it. It's not what everybody recommends. But especially since the new SFTP doesn't have that line ending translation, I think it's just a good idea. So, now that we have that all straightened out and you've got your text editor opened with this file, let's take a quick look at the program itself. This line up at the top here is called the shebang line, and that's because it has this hash mark and then this exclamation point, which in typesetting circles and in other circles is sometimes called a bang.
And so, that's pronounced as shebang. And then, it has the path to Perl. Now, again, this only applies UNIX systems, but everybody always leaves this at the top of their Perl program, so I just suggest that you do. And it's also understood by the Perl interpreter itself, so that it can understand the switches that come after it. And we'll talk more about these switches in our section on security, but that -w switch is a really good idea. So, that's the part of the program that says this is a Perl program.
These other lines that have the hash marks at the beginning of them, those are comments. This is technically also a comment, but it's a really special one, it's the shebang line, and it has to be the first line in the file. The rest of these, anything with a hash mark added, anything after a hash mark anywhere on a line until the end of the line, there's those line endings again, until the end of the line is considered a comment. They're just ignored by the Perl interpreter. And so, you can put anything you want there. I tend to put copyright notices and things like that.
Some of my other more complicated programs, I'll put in comments that explain what some of the code does. This is actually a very short and simple program, and so I didn't find any need to put in any comments in any place else. Now, the rest of this is the Perl program itself. And we'll talk a little bit about some of this. The point of this course is not really to teach you how to write programs in Perl, but rather how to modify things in Perl. But, for the purpose of this little exercise right here, I just want you to get used to getting in and editing something and watching it work because I think that is a important hurdle to get over if you're never written any program code before.
So, what I'm gonna ask you to do is look for the line in your program that says "Content-Type: here, print "Content-Type:. It says text/plain. And what that does is that sends a CGI header, so that the server knows this is a CGI program, and it can do something with it. It also tells the web browser when it gets over to your web browser that this is not HTML, but it's plain text. Most of the time, these will say text/HTML. This time it says text/plain because we're trying to make the simplest program we can here.
So, what I want you to do is put in a couple of blank lines there. And then, move your cursor up and type what I type. I'm going to say my $name = "and then type in your name and put in another ";. Now, that semicolon in Perl is what you end a command with. Now technically, it's a separator, but you can think of it as ending the command.
At the end of every command, you're going to put a semicolon like that. And my $name, that declares a variable, a variable name. And = gives it a value. And the value is this stuff in the quote marks here and just put your name in there. And then, what you're going to do is you're gonna open up another line and type print "$name\, not a forward slash, and the letter n.
And that \n, it means a new line. It ends a line there. And then another ";, and that's all we're going to put in here. And what that'll do it'll just print your name and another blank line, and it'll print all the stuff that's in here. Now, you want to Save the file and Close it. And open up your file transfer program.
And go ahead and Connect to your server just like we did before. And this is the part we've done before, so this should be at least a little bit familiar. And change over to the cgi bin directory. And you've already got your test.cgi program in there, and it's got its file permission set right. And then, we're going to come over here to the test-cgi folder on the local side, and you'll notice that the size is now different because we've made changes. And just transfer this over like that.
Now, look at this, it's gonna warn me that I have already a file with the same name on the server, and are you sure you want to Overwrite it? Well, this is what I intend to do, so I'm gonna go ahead and say OK. And now, it has gone ahead and put it up there. Now, in my case, it left the permissions alone, but it's always a good idea to check anyway. At least the first few times until you know exactly how it works. And make sure those Execute permissions there are still set, and they are, so we can say OK or Cancel.
Either way works with... I said OK, so it actually went out there and changed it again. You see that? So, now we're done with that, and we can open up our web browser. And it should come up and do what we expect it to do. It should say your name there, so I'm just going to put it in here. Now, this is mine. You, of course, have it on your server at a different URL there, whatever it is for yours.
And I found it right there. It's the CGI den one. It's that one there. And there it is. And you'll notice at the top there, it has my name. So, assuming that worked for you, you have now modified a Perl script. You've written a little bit of Perl code. You've uploaded it to the server, and it works. I think you should treat yourself to a candy bar or whatever it is you reward yourself with when you do something cool 'cause you've just done something cool. In case it isn't working, our very next lesson is on what to do when it doesn't work.
And so, we'll get to that next. But for now, congratulations, you have modified a Perl script and made it run on your server.
Download the scripts used in the movies from the Exercise Files tab.
- What is Perl?
- What is CGI?
- Installing a Perl script
- Installing a hit counter
- Installing a contact form script
- Installing a blog script
- Security issues