1 Introduction to CGI and Perl CGI/Perl Programming By Diane Zak 1 1 Objectives • In this chapter, you will: • Review basic Internet terminology • Learn about the CGI protocol • Create a CGI script using the Perl language • Run a CGI script from the command line and Web browser • Debug a CGI script 2 1 Internet Terminology • Internet – Global network that connects millions of computers and many networks together • IP Addresses – IP = Internet Protocol – Used to uniquely identify machines on the Internet – Made up of 4 sets of numbers separated by periods – Example: 216.148.218.195 3 1 Internet Terminology • Domain Name – Friendly name that is easier to remember than an IP address – Association of a name to an address – Example: www.redhat.com is associated with 216.148.218.195 4 1 World Wide Web (WWW) • Web browsers are used to view Web pages written in HTML that have been served up by Web servers – HTML = Hypertext Markup Language – Web pages have .html or .htm extensions and are stored on web servers 5 1 World Wide Web (WWW) • URLs are used to tell the browser exactly which web page to request – URL = Uniform Resource Locator 6 1 World Wide Web (WWW) • URLs are made up of 4 parts: – Communication Protocol • Typically http:// – HTTP is the default communication protocol, so URL www.redhat.com = http://www.redhat.com – Web server domain name • IP address or domain name of the server – Path or directory (optional) • A specific directory on the web server – Web page filename (optional) • A specific file on the web server • If a filename is not typed, a default document (if it is known to the web server) will be sent 7 1 Static Web Pages • A static web page will only change if the page is manually edited or updated • Usually used for information that doesn’t change often • Not interactive • Most web pages are static 8 Dynamic Web Pages 1 • Interacts with user • Uses programs or scripts to create a dynamic HTML page using CGI, ASP, Java applets, etc. – Programs: • Compiled beforehand, so the computer can read the code • Faster than scripts – Scripts: • Each line is converted into code as the script is running • Easier to create and change • Works on most platforms or operating 9 systems without much updating 1 Introduction to CGI • CGI = Common Gateway Interface – Protocol that allows communication between web server and CGI scripts – CGI scripts usually stored in cgi-bin directory – Scripts usually have .cgi or .pl extensions – Can use scripting languages or compiled languages to create CGI scripts • Scripting languages: Perl, AppleScript, UNIX shell • Compiled languages: C, C++, Visual Basic 10 1 How CGI Works • Web browser requests CGI script from web server using HTTP protocol • Web server sees that it is a CGI script and runs the script • The CGI script sends output back to the web server which sends the output to the browser 11 1 First CGI Script and Perl • Finding Perl interpreter – Need to know where the Perl interpreter is, to include in your program – whereis perl (UNIX) – Start --> Search --> For Files or Folders --> perl.exe (Windows) 12 1 First CGI Script and Perl – shebang line - tells the operating system where the Perl interpreter is located necessary on UNIX – comment line – ignored by the Perl interpreter – End statements with semicolon – newline character – to end a line and 13 create a new line 1 First CGI Script and Perl • Content-type header – tells the script what type of output there will be – print “Content-type: html/text\n\n”; • print is used to output information within the quotation marks to the browser • HTML tags are created dynamically using print statements 14 1 Testing a Script 1. Open shell or DOS prompt 2. cd path path=directory where the Perl script is located Example: cd cgi-bin/chap01 3. perl -c filename OR perl -w filename filename = the name of the Perl script Example: perl -c first.cgi or perl -w first.cgi -c option or switch will check for syntax errors but will not execute the script -w option or switch will check for execution15 errors and will execute the script 1 Testing a script Results of perl -c and perl -w on UNIX Results of perl -c and perl -w on Windows 16 1 Testing a Script • Additionally, can test a Perl/CGI script with a Web browser – On a UNIX machine, change the file permissions of the script so that it can be executed • chmod 755 filename – In the browser, type in the URL of the script: • http://yourservername/cgi-bin/chap01/first.cgi 17 1 Testing a Script Results of testing the first.cgi script on a Web browser 18 1 Debugging a Perl Script • When you use the -c or -w switches with Perl, the line number of the error will be shown – Edit the Perl script, and go to that line number – Examine that line, and the line directly above it – Make the corrections, resave the file, and try testing the script again 19 1 Debugging a Perl Script • Look for simple errors, such as: – Missing semicolons at the end of lines – Uppercase commands or functions (remember that Perl is case-sensitive) – Missing quotation marks at the beginning or end of print statements 20 1 Summary • Web pages are either static or dynamic. • The contents of a static web page are established when the page is created. A web server will transfer the contents of a static page to a browser, and the browser will interpret and display the HTML instructions or code. • Dynamic web pages interact with the user, and will use an HTML document and 21 a program or script. Summary 1 • The CGI protocol defines how a web server communicates with CGI scripts or programs. • UNIX and Perl are both case-sensitive. • Perl CGI scripts on UNIX must start with the shebang line, which tells the location of the Perl interpreter • The Content-type header is necessary to send HTML output to a browser: – print “Content-type: text/html\n\n”; 22 1 Summary • The newline character is \n. • perl -c scriptname will check for syntax errors in the script, but will not execute the script. • perl -w scriptname will check for execution errors in the script, and will also execute the script. • Perl statements must end with a semicolon (;). 23