Siaris
Simple Things
Syndicate: full/short
Siaris
Categories
General0
News2
Programming2
LanguageBits0
Perl50
Ruby10
VersionControl1
Misc1
Article Calendar
<= May, 2008
S M T W T F S
123
45678910
11121314151617
18192021222324
25262728293031
Search this blog

Key links
External Blogs
Brought to you by ...
Ruby
1and1.com

Simple Object Oriented Tutorial (soot part I)

Andrew L. Johnson (First published by ItWorld.com 2001-05-31)

Object Oriented Programming (OOP) means a lot of different things to a lot of different people — but for our purposes let’s just say that OOP is simply a way to create a "thing" that your programs can use (obviously, this is rather vague at the moment).

Before we delve into making a simple object, let’s look at using an object and explore a few concepts. Consider a radio, the kind your grandfather might have had — a large wooden box with a speaker, a volume dial, and a tuning dial with the frequencies shown. We do not necessarily need to know what the insides look like, or how it functions, we need only know how to operate this device (we need to know how to use the device’s interface). Radio’s have changed a great deal since those days — instead of tubes inside we have solid-state circuits — and they are generally much smaller. But the interface has hardly changed at all: We still have a speaker, and volume and tuning controls (though we may have a digital display of the radio frequencies, and the controls may operate by mere touch rather than actually turning a dial). Everything on the inside has changed, but the only difficulty grand-dad might have using it is finding a station that played something he could stand to listen to. We will return to this radio analogy as we progress.

Now let’s look at using an existing object oriented module in Perl. The LWP modules provide us with easy methods of fetching web pages from the Internet (the modules are available in the libwww package on CPAN). The following is a very simple script to fetch a web page, based on an example in the lwpcook.pod documentation that comes with the module set:

    #!/usr/bin/perl -w
    use strict;
    use LWP::UserAgent;

    my $ua = LWP::UserAgent->new();
    $ua->agent("TestBot/0.1"); # pretend we are very capable browser

    my $req = HTTP::Request->new('GET' => 'http://www.perl.com/');
    $req->header('Accept' => 'text/html');

    # send request
    my $res = $ua->request($req);

    # check the outcome
    if ($res->is_success) {
       print $res->content;
    } else {
       print "Error: " . $res->status_line . "\n";
    }

To fetch a web page, we have to open a socket and communicate with a web server on a particular port number and request the page using the HTTP protocol. But conspicuously missing from the above program is anything resembling socket handling or communication with a web-server. Instead we created two objects, told those objects what we wanted, and they handled all the messy details. All we needed to know was how to use the objects themselves — just like I do not need to know how my radio actually tunes in radio stations and plays music seemingly out of thin air, I only need to know how to operate the controls.

We first pulled in the LWP::UserAgent module (this provides with the "agent" that does the talking to a web-server), which also pulls in the HTTP::Request module. We obtained a new UserAgent object by calling the new() method of the LWP::UserAgent class.

Brief working definitions: A class is something that defines objects and their methods; a method is simply a subroutine that is connected to a class or objects defined by a class. We will get to these in a later segment of the tutorial.

The documentation tells us that we should give our agent a name using the agent() method (called as: $ua->agent("name/version")). Next we construct an HTTP request (what web page are we requesting), by creating a new() HTTP::Request object and telling what URL we want to GET. We also use the request object’s header() method to tell it we will Accept a text/html document.

Internally, these two objects "know" what to do when their methods are called, we only have to press the right buttons or turn the right dial. So, now that we have an agent and a request, we ask the agent to make the request() and assign the result to a new variable. This result is also an object (we did not create it, the request method gave it to us) that knows if it was successful or what error might have occurred. We need only test it for success, or print the error.

In the real world, we might have a radio tuned to a particular station, but that doesn’t help us hear a particular song — for that we’d need to make a request to the station (the radio won’t help us here). We may call the station on the phone, talk to the DJ to make our request, and he may tell if he’ll play the song and we can sit back and wait for it. If we had a box next to our radio that allowed us to type in a radio station and a song request and just hit a button, that would not be dissimilar to the above snippet to fetch a web-page. All the messy details of phone numbers and talking to the DJ would be hidden from us, and we’d simply have to know what buttons to push on our little ‘Radio::Song::Request’ black box.

This initial segment of our tutorial is only meant to give you a handle on the basic concept of an object. In subsequent installments we will begin building our own little black box.