150 likes | 266 Views
Introduction to Object-Oriented Perl and CGI.pm. Instructor: Dr. Joseph DiVerdi. Object-Oriented Perl. Not a class in Object-Oriented Programming Emphasis is on use of Object-Oriented Perl Modules rather than on the creation of them
E N D
Introduction to Object-Oriented Perl and CGI.pm Instructor: Dr. Joseph DiVerdi
Object-Oriented Perl • 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, well-documented interface: a set of actions which can manipulate these data structures
Some Definitions • 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
Some Definitions (con’t) • 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
An Example • 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
Another Example • 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
References • 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);
Using Net::FTP.pm • 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";
Using Net::FTP.pm (con’t) 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"; exit;
Simple HTML • 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/
Simple CGI • 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”
Simple CGI (continued) #!/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__
Simple CGI (continued) • Use web browser to access URL “http://www.verinet.com/cgi-bin/cgiwrap/web0/first.pl