1 / 23

Introduction to CGI and Perl

Introduction to CGI and Perl. CGI/Perl Programming By Diane Zak. 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.

oria
Télécharger la présentation

Introduction to CGI and Perl

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to CGI and Perl CGI/Perl Programming By Diane Zak

  2. 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

  3. 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

  4. 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

  5. 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

  6. World Wide Web (WWW) • URLs are used to tell the browser exactly which web page to request • URL = Uniform Resource Locator

  7. 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

  8. 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

  9. Dynamic Web Pages • 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 systems without much updating

  10. 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

  11. 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

  12. 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)

  13. 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 create a new line

  14. 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

  15. 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 execution errors and will execute the script

  16. Testing a script Results of perl -c and perl -w on UNIX Results of perl -c and perl -w on Windows

  17. 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

  18. Testing a Script Results of testing the first.cgi script on a Web browser

  19. 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

  20. 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

  21. 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 a program or script.

  22. Summary • 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”;

  23. 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 (;).

More Related