Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Classism - Page 170

May 21, 2001

If you were at all inclined to flaunt your expertise of OOP at, say, a social gathering on a transoceanic cruise line, there are three terms you'll need to toss about: class, instance, and method. This trilogy of lingo forms the backbone for any object oriented model.

A class is abstract, a category which may be defined loosely or narrowly. It is so vague in fact that we can only define a class by example. "Furniture" may be a class. "Cars" may be a class. Hold onto this thought.

An instance is a single object which is a concrete embodiment of its class. A dining table may be an instance of the furniture class. A 1978 orange Chevy Nova may be an instance of a cars class.

There are few cognitive bounds to classes and instances. "Dining tables" could be a class itself, and a Victorian Rosewood circular dining table from 1852 may be an instance of this class.

The methods of an object are the levers, buttons, and knobs of the black box — the ones we used to turn up the stove, or nuke a potato in the microwave.

Methods come in two flavors: class methods are levers that operate at the level of the class without regard to a specific instance of an object. Logically, instance methods are levers that operate only on a specific object derived from a class, and not on the class as a whole.

In Perl, a class is drawn from a package or module (which itself is a package). Consider then the CGI and DBI modules, both of which we've relied on throughout this series, for Web-based input/output and database interaction respectively. Both of these modules are also object classes, not unlike "furniture" or "cars". Hold that thought!

That New Instance Smell

By including the CGI or DBI Perl module into a script, we've also conjured up its class. All along when we've been writing:

use CGI;
use DBI;

We've unwittingly been making the CGI and DBI classes available to us. But there isn't a whole lot you can actually do with a class, since it's just a category, a concept, or a notion. You could walk into the local Ethan Allen store and tell the clerk "I want some furniture", but at some point in the conversation you're going to have to identify what furniture — specific instances of furniture that you want.

This is why the most popular class method is the constructor — wherein you tell the class to stamp out a single instance object for you to work with.

my $cgiobj = CGI->new;

Object methods are triggered with the -> syntax, following the format classname->methodname(parameters). Of course, the parenthetical list of parameters can be left out if there aren't any. Here, we trigger the CGI class to stamp out an instance object with which we can do actual work. The object is assigned as a reference to a scalar value, here descriptively named $cgiobj.

Most of the methods available to CGI are instance methods, meaning that they apply to $cgiobj rather than CGI. For example, we can spit out an HTML header by triggering the header() method:

print $cgiobj->header;

Or, to override the default parameters for this method:

print $cgiobj->header(-expires=>'-1d');

In the case of DBI, the typical constructor looks a little different. DBI is a class for interacting with a database, generally by passing on SQL queries and receiving the results of those queries. To open a channel of communications with a database, we need to use a database handle, and this handle is in fact an object, which is an instance of the DBI class.

my $dbh1 = DBI->connect('dbi:mysql:dbase1','login','pwd',
  {PrintError=>1,RaiseError=>1});

[The lines above are one line. They have been split for formatting purposes.]

For the DBI class to stamp out a database handle object, it needs to establish a connection to the database, hence the DBI constructor is the connect() method. This method takes several parameters needed to make the connection. Also notice the last parameter in the connect() call, which is a hash that informs the handle object how to deal with errors.

Because a class is generic in nature, we're not limited to stamping out a single object. In the same script, we could create an entirely independent database handle object from the same DBI class that opens a connection to a different database:

my $dbh2 = DBI->connect('dbi:mysql:dbase2','login','pwd',
  {PrintError=>1,RaiseError=>1});

[The lines above are one line. They have been split for formatting purposes.]

You can imagine for example that we might read data from the database attached to $dbh1 and write some of it to the database attached to $dbh2.

To work with any one of the databases, though, we'd now rely on the handle object. For instance, to send a query to dbase1 we could call the do() instance method:

my $sth = $dbh1->prepare("select * from table1");

Interestingly, this instance method actually returns a statement object, which we assign to $sth and which has its own instance methods (for pulling the data out of the statement, such as the fetchrow() method).

The Perl You Need to Know Part 24: Introduction to Object Oriented Perl - Page 169
The Perl You Need to Know
A Matter of (Indirect) Syntax - Page 171


Up to => Home / Authoring / Languages / Perl / PerlfortheWeb




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers