oop_and_cgi - XTR Systems, LLC

Download Report

Transcript oop_and_cgi - XTR Systems, LLC

CSU - DCE 0791 - Webmaster
JavaScript Class - Fort Collins, CO
Introduction to
Object-Oriented Perl
and
CGI.pm
Instructor: Dr. Joseph DiVerdi
Copyright © XTR Systems, LLC
Object-Oriented Perl
CSU - DCE 0791 - Webmaster
JavaScript Class - Fort Collins, CO
• Not a class in Object-Oriented Programming
• Emphasis is on use of Object-Oriented Perl
Modules rather than on the creation of them
• Perhaps the most important component of
Object-Oriented Programming
– Encapsulation - the hiding of actual data
structures, exposing instead a limited, welldocumented interface: a set of actions which can
manipulate these data structures
Copyright © XTR Systems, LLC
Some Definitions
CSU - DCE 0791 - Webmaster
JavaScript Class - Fort Collins, CO
• object:: a data structure with a collection of
behaviors that can be performed on it
• method: a behavior performed on an object
• class: a definition of methods
• inherit: receive definitions
Copyright © XTR Systems, LLC
Some Definitions (con’t)
CSU - DCE 0791 - Webmaster
JavaScript Class - Fort Collins, CO
• attribute: a property of an object
• Identity: each object, although a member of a
class and possessing similar attributes, is
unique and unto itself
• constructor: a behavior which sets up an
object and initializes its data structures
• destructor: a behavior which performs clean
up on an object before the object is destroyed
Copyright © XTR Systems, LLC
An Example
•
•
•
•
CSU - DCE 0791 - Webmaster
JavaScript Class - Fort Collins, CO
My Miata is an object
It is a member of the class - Car
It has attributes - red, convertible
It is capable of behaviors - steering,
forward motion, backward motion
Copyright © XTR Systems, LLC
Another Example
CSU - DCE 0791 - Webmaster
JavaScript Class - Fort Collins, CO
• A particular rectangle is a member of a class
rectangle, it has attributes such as lower-left
and upper-right corners, and a color, it is
capable of certain behaviors like calculating
its area
Copyright © XTR Systems, LLC
References
CSU - DCE 0791 - Webmaster
JavaScript Class - Fort Collins, CO
• Objects are generally (but not exclusively)
identified by references
– References are easy to sling around independent of the size of the referred structure
• Typical syntax:
– “class method”
my $object = Class_name->new_method();
my $object = Class_name->new_method(arg1);
– “object method”
$return_value = $object->method_name();
$return_value = $object->method_name(arg1, arg2);
Copyright © XTR Systems, LLC
Using Net::FTP.pm
CSU - DCE 0791 - Webmaster
JavaScript Class - Fort Collins, CO
• Bad module!
– Cannot run under “-w”
– Can run under “use strict”
#!/usr/bin/perl
use strict;
use Net::FTP;
my $port = Net::FTP->new("ftp.verinet.com");
die "'new' failed because $@\n" unless $port;
$port->login('anonymous',
‘your_address@your_domain.top_domain’) or
die "Can't login.\n";
Copyright © XTR Systems, LLC
Using Net::FTP.pm (con’t)
CSU - DCE 0791 - Webmaster
JavaScript Class - Fort Collins, CO
print "\nCurrent working directory is '", $port->pwd(), "'.\n";
print join("\n", $port->dir()), "\n";
$port->cwd('pub') or
die "Can't change working directory.\n";
print "\nCurrent working directory is '", $port->pwd(), "'.\n";
print join("\n", $port->dir()), "\n";
$port->cwd('linux/www/browsers/netscape/4.04/base_install') or
die "Can't change working directory.\n";
print "\nCurrent working directory is '", $port->pwd(), "'.\n";
print join("\n", $port->dir()), "\n";
$port->get('README.txt') or
die "Can't get a file.\n";
$port->quit() or
die "Can't quit.\n";
Copyright © XTR Systems, LLC
exit;
Simple HTML
CSU - DCE 0791 - Webmaster
JavaScript Class - Fort Collins, CO
• Create a directory named “html” in top level directory,
e.g., “/users/web0/html” and change mode to “755”
• In “html” create a file named “index.html” and change
mode to “644”
<html>
<head>
<title>Simple web page</title>
</head>
<body>
This is a web page.
</body>
</html>
• Use web browser to access URL
“http://www.verinet.com/~web0/
Copyright © XTR Systems, LLC
Simple CGI
CSU - DCE 0791 - Webmaster
JavaScript Class - Fort Collins, CO
• Create a directory named “cgi-bin” in top level
directory, e.g., “/users/web0/cgi-bin” and change
mode to “755”
• In “cgi-bin” create a file named “first.pl” and change
mode to “755”
Copyright © XTR Systems, LLC
Simple CGI (continued)
CSU - DCE 0791 - Webmaster
JavaScript Class - Fort Collins, CO
#!/usr/bin/perl -w
print <<__HTML__
Content-type: text/html
<html>
<head>
<title>Simple web page</title>
</head>
<body>
This is a web page.
</body>
</html>
__HTML__
Copyright © XTR Systems, LLC
Simple CGI (continued)
CSU - DCE 0791 - Webmaster
JavaScript Class - Fort Collins, CO
• Use web browser to access URL
“http://www.verinet.com/cgi-bin/cgiwrap/web0/first.pl
Copyright © XTR Systems, LLC