Title: Introduction to Programming the WWW I
1Introduction to Programming the WWW I
- CMSC 10100-1
- Winter 2003
- Lecture 14
2Introduction to Perl
- What is Perl?
- How do you run it?
- From command line
- From Web
- How do you write Web pages with it?
3Accessing HTML Files Over the Internet
4Web Application Program
- Carries out many dynamic tasks, such as the
following - Input a search term, search the WWW, and return
the results - Calculate and display the number of times that a
page has been viewed - Verify the input fields on a Web form
- Save a Web form into a database
- Display a special graph, or return the results of
a calculation based on data input from a form
5The Common Gateway Interface
- a standard that enables Web browsers to exchange
data with computer programs located on a Web
server - first appeared in the NCSA HTTPD Web server
software built by the National Center for
Super-computing Applications (NCSA). - one of the first widely used Web servers
- was simple and the program source code was made
available for free - It is simple to use and available on a variety of
Web servers.
6How Browsers and Web Applications Work with CGI
7An Interface with Different Programming Languages
- Web application programs that are developed
specifically to work with the CGI standard are
known as CGI programs. - Lots of different programming languages can be
used. For example, - Perl,
- Visual Basic,
- Java,
- C,
- C,
- and UNIX shell scripts.
8The Perl Programming Language
- Practical Extension and Reporting Language
- invented in 1987 by Larry Wall at NASAs Jet
Propulsion Laboratory - developed as a utility programming language for
the UNIX operating system - gained popularity because of its ease of use,
free availability via the Internet, and its
powerful combination of features
9Why Perl is Popular
- Perl is a free language with lots of free
applications - Perl is easier to work with than many other
languages - Perl provides a CGI interface module
- Perl applications are portable.
10Internet Service Provider Issues
- Some things to determine with your ISP
- Allow CGI programs to execute on its Web-server?
Does it have Perl? What Version? - Login and initial password on the Web server?
- Where do you put your Perl programs on the Web
server and what permission settings? - How much disk space for publishing?
- Support FTP and/or Telnet access? or SSH/SCP?
11Summary
- Web pages written in HTML are static and cannot
interact with users. - CGI is an interface standard that allows
computer programs to communicate with Web
servers. Several programming languages can be
used with CGI.
12What You Need to Get Started
- SCP/SSH
- Connecting to the Web server
- Setting up your directories.
- Getting the location of the Perl interpreter.
13Navigating UNIX Directories
14Navigating UNIX Directories - 2
15Finding The Location Of Perl
- Perl interpreter
- A program that translates Perl program commands
into commands that are understandable to a
computer. - Runs your Perl programs and generates any output.
- Its command name is simply perl.
- It can be installed in any of several places on a
Web server.
16Finding Location Of Perl
- SSH onto Web Server enter
- which perl
- where is perl
Perl Interpreter Location
17Starting Your Program Development Process
- Each time you develop and run a program
- Create a program file and copy (or save) it into
the correct directory. - Change your programs access permissions.
- Check your programs syntax.
- Run your program.
18Create Your Program File
- Editors are computer applications that enable you
to create, change, and save files - Microsoft Windows, Notepad is a simple editor
that works well for Perl development. - On UNIX systems, the Pico, Vi, and Emacs editors
are popular choices.
19Starting Your First Program
- Start Editor and enter the following
- !/usr/local/bin/perl
- This program prints out a simple message
- print Steady Plodding Brings Prosperity\n
20Program Entered in Pico
21Run The Program
- Save the program file on the web server.
- Enter the full path to the program file to run.
- For example
- /home/kirby/html/cgi-bin/simple1.cgi
Home Directory
Directories On Web Server
Program File
22Change the Programs Permissions On A Unix Web
Server
- UNIX access permissions are used to define the
access rights of your files - read permissions define if the file can be read
- write permissions define if the file can be
changed, - execute permissions define if the file can be
executed as a program - You set access permissions for your user ID, your
user IDs group, and everyone else
23To Check Your Program Syntax(on a UNIX Web
Server)
- establish an SSH session,
- navigate to the directory that contains the file,
- enter perl c filename, where filename is the
program file whose syntax you want to. For
example, - cd html/cgi-bin
- perl c simple1.cgi
- If no syntax errors then you will seed
- simple1.cgi syntax OK
24Program with Syntax errors
Missing quote mark
25Running Your Program
- At least two different ways to run your Perl
programs - Directly on a Web server or PC without a browser
- Using your browser over the Internet.
26Getting Ready to Run Your Program Over the
Internet
- 1. To use a browser over the Internet, add the
following MIME content-type line - print Content-type text/html\n\n.
- !/usr/local/bin/perl
- print Content-type text/html\n\n
- This program prints out a simple message
- print Steady Plodding Brings Prosperity\n
27Change Program Process
- 1. Edit the program
- 2. Change the program.
- 3. Save the file.
- 4. Check the programs syntax.
- 5. Run the program.
28Running Your Program Over the Internet
- 1. Connect to the Internet.
- 2. Start your browser.
- 3. Enter the URL or Web address to your file
- 4. Check the programs syntax.
- 5. Run the program.
- For example, assume saved the in a file called
simple2.cgi in my cgi-bin directory on the Web
server. Can execute by the following - http//people.cs.uchicago.edu/kirby/cgi-bin/simp
le2.cgi
29Output Of Program Executed Over the Internet
30Dealing with Problems
- Many Web servers redirect the errors from CGI
programs into a separate error log located on the
server. - You may receive a generic, cryptic message when
running programs with errors. - Two common messages are Internal Server Error
(Figure 2.17) and 500 Server Error.
31Some Things to Check
- Verify the program syntax.
- Verify the access permission.
- Verify the file has the proper extension (.pl or
.cgi). - Verify the program is stored in the correct
directory. - Verify the correct Web address to your program.
- Verify the first line has the correct of the Perl
interpreter. - Confirm the accuracy of your MIME Content-type
line.
32An Internet Server Error
33Generating HTML Statement from Perl Programs
- !/usr/local/bin/perl
- print "Content-type text/html\n\n"
- print "ltHTMLgt ltHEADgt ltTITLEgt Example
lt/TITLEgtlt/HEADgt" - print "ltBODYgt"
- print "ltBgtltFont Size5gtThis is a Test
lt/FONTgtlt/Bgt" - print "A very Interesting test"
- print "lt/BODYgtlt/HTMLgt"
34Program Output
35Another approach
- !/usr/local/bin/perl
- print "Content-type text/html\n\n"
- printltltEND
- ltHTMLgt
- ltHEADgt
- ltTITLEgtExamplelt/TITLEgt
- lt/HEADgt
- ltBODYgt
- ltBgtltFont Size5gtThis is a Test lt/FONTgtlt/Bgt
- A very Interesting test
- lt/BODYgtlt/HTMLgt
- END
36What is this?
- Everything between ENDs gets treated as if it
were inside a double quote - Use this with require to print out a page with
a fixed header, footer, body - Use require foo.pl to execute the code in that
file in that spot
37Summary
- There are several different configurations you
can use to develop CGI/Perl programs. - Using FTP and Telent are common
- Steps to create a program create with editor,
enter program, set permissions, check syntax, and
run the program. - Two statements are required
- First line identifies Perl interpreter location.
- Second line specifies the MIME Content-type.