Concepts / Running your first script on a web server
Subtitles of the Movie
So far so good; what about trying it for the web. What we are going to do first of all is to save our file to the correct directory for our local web server to locate it. And you will need to change the path to suit wherever you've installed your local web server software. You are looking at putting your Perl scripts onto a remote host, and testing them through there, then we are going to be looking at the ways to do that in the next movie. For now let's save that into the cgi-bin directory under the document route, then we are going to call it 'hello.cgi' this time. So we've saved our file and now let's move over to our web browser, and I am using Mozilla, but any web browser is the same. And we are going to try to run that exact same file. Now this is not going to work. I want to highlight differences here between running scripts from the command line and running them through the browser, and making your request to the server. When we ask for hello.cgi, we get an internal server error. And because of the way that web server and Perl has been setup, the error is saved off to a log file somewhere. So let's go back to our script and see what could be causing the problem. A single command here simply prints 'hello world'. What we need to add is another line to tell, first of all, to tell the web server what kind of content it's delivering to the browser. So just type this line in exactly as I have written it, all the hyphens, colons, spaces, and slashes the right way; and essentially this is the line, we weren't looking exactly in too much detail about mime headers and so on, but this is a line that will tell the web server what kind of content is serving. You will notice one other thing about our script. We've also changed the extension of it, we've called it 'hello.cgi'. This is because we are not using the Windows file association, to process the script as a Perl script. What we need to do instead, is we need to include a line right at the very, very top of this script, to make sure that when the web server finds the script and tries to read it, it knows that it's a file that needs to be run through the Perl executable. So this is known as the shebang line, a hash followed by an exclamation mark or a bang, and then we need to put in the complete path to wherever it was that we installed the Perl executable. In this case I installed it under the directory, where I installed Perl, it's in binary, binary directory or Bin, and it's Perl.exe - again because we are running on a windows system. So let's return to my web browser and try again. Success, we've put the 'hello world' output through the web server and back to the browser. Once again we are not going to worry too much about the technicalities, what those lines mean, or what they are doing. But for now it's enough just to get the script up and running.
Tutorial Information
| Course: | Perl Fundamentals |
| Author: | Joshua Mostafa |
| SKU: | 33403 |
| ISBN: | 1-9320-7215-2 |
| Release Date: | 2002-12-19 |
| Duration: | 7.5 hrs / 113 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 