Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Recently, I received an e-mail from a gentleman in the UK who was asking questions about how to set up a CGI programming environment on a Windows box. Below is an edited copy of my reply. It's an "Idiot's Guide" to getting started with CGI programming with Perl on Windows. I present as a way to get started, but not as "the" way to get started. Hopefully Monks who are totally new to this process can use this information.


First, you need to get Perl if you do not already have it installed. Go to http://www.activestate.com/Products/ActivePerl/Download.html and download the latest release for Windows. As of the time I write this, it's a link near the top that reads "Windows MSI". If for some reason you cannot install an MSI package, you can use the "Windows as Package" installer below that (but it's larger and you will have difficulty uninstalling it). If you do not enjoy the luxury of a high-speed Internet connection, you can find a smaller download near the bottom of the page at "Intel, APi522e.exe". This is about 2 to 3 megs smaller than the releases above. It is only Perl 5.005 (the others are 5.6), but the majority of Perl programs are still written in 5.005, so that's not an issue. However, some of the latest and coolest stuff you can do with Perl won't be available. If you're new to Perl, it will be a long time before you worry about stuff like that.

Once downloaded, double-click on the executable to install it. Accept all of the defaults, if possible.

Next, you need a Web server. I recommend you dowload Apache for Windows. Go to http://www.apache.org/dist/ and find "Windows Installer". It's about a 3.6 meg download. Double-click the icon after downloading and select the defaults. When it asks if you want a "Typical", "Full", or "Custom Install", choose "Typical" and let it do its thing. If all is successful, Apache will be on your system.

Go to C:\Program Files\Apache Group\Apache and run the Apache.exe program. Congratulations! If all went well, you have a Web server running on your box. Once the server is running, open a Web browser and type http://localhost/ into the address bar. You should see a Web page telling you that your installation of Apache was successful.

If there was a problem with the Apache install, often the error message and the window it appears in will dissappear quickly (assuming that you double-clicked the executable and didn't run it from the command line). To see the message, open a command prompt and CD to the directory. Then, run the program from the command line. If there is an error message, you'll see it and take the appropriate action.

Next, you want to ensure that you can run CGI scripts. First, go to C:\Program Files\Apache Group\Apache\cgi-bin and create a file named "test.cgi". Assuming that you have followed the defaults for your Perl installation, edit test.cgi and enter the following program:

#!C:\perl\bin\perl.exe -wT use strict; use CGI; my $query = new CGI; print $query->header( "text/html" ); print <<END_HERE; <html> <head> <title>My First CGI Script</title> </head> <body bgcolor="#FFFFCC"> <h1>This is a pretty lame Web page</h1> <p>Who is this Ovid guy, anyway?</p> </body> </html> END_HERE # must have a line after "END_HERE" or Perl won't recognize # the token
It's important to remember that you must have a line (even a blank one) after the final END_HERE. Heck, hit return a couple of times just to be safe. Once you have that entered exactly as I have it above, enter http://localhost/cgi-bin/test.cgi into the browser's address bar and hit return. If all went well, you'll see a Web page telling you how lame it is.

If for some reason you could not accept the defaults for installing Perl, make sure that the first line of the Perl script points to the correct Perl executable. That's how Apache knows how to run this program.

If END_HERE is the last line of the program and has no return after it, Perl will complain that it

Can't find string terminator "END_HERE" anywhere before EOF at c:/prog +ram files/apache group/apache/cgi-bin/test.cgi line 8.
Further, you'll just see an "Internal Server Error" or something similar in your browser. To find the actual error message, you'll go to C:\Program Files\Apache Group\Apache\logs and open up the "error.log". In fact, open up this log any time you want to figure out why your CGI program didn't run.

The above instructions should work fine if you are one Windows9.x or WindowsNT (you might need administrator privileges for WinNT).

If you are interested in books on the subject, you'll want Learning Perl for Win32 Systems (published by O'Reilly press). Also, CGI Programming with Perl, second edition (also by O'Reilly) is an excellent resource, but assumes that you already know Perl. You can find Perl books and related merchandise at this link.

Currently, almost every other book out there (there are a couple of exceptions) that explains CGI programming is simply miserable. Sorry to be rude, but there is no other way to explain it. One book to stay away from in particular is "Perl and CGI for the World Wide Web, Visual Quickstart Guide" by Elizabeth Castro. This book has been wildly popular and demonstrates not only poor Perl programming techniques, but many of her scripts have terrible security holes that are likely to get new programmers fired and their Web sites cracked.

This little example won't teach you to write CGI scripts, but it will get you started. If you are interested, check out my online Web programming course for further information on this subject.

Update: I have just upgraded to Perl v5.6 on my home machine only to discover that for some reason, I had to edit the autoexec.bat file to add C:\PERL\BIN to the path. If you aren't sure how to do this, grab a different version of the Perl installer as it's very easy to damage things while editing autoexec.bat.

Update 2: Another book should be added to the list of recommended books. "Writing CGI Applications with Perl" by Kevin Meltzer and Brent Michalski, published by Addison Wesley. From my initial readings, it appears to be very good.

Update 3: I warn the reader to stay away from "Perl and CGI for the World Wide Web" by Elizabeth Castro, but I now cautiously recommend the Second edition (not the first!). The second edition is much better than the first and while it has some security issues, it's a good introduction to Perl/CGI.


In reply to Setting up Perl/CGI with Windows by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (13)
    As of 2014-04-18 22:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (472 votes), past polls