Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OOP ??? Having problems understanding this fully. 3

Status
Not open for further replies.

garymgordon

Programmer
Apr 5, 2000
307
US
Questions ...

1) What is a 'blessed' reference? (And, where does this name 'blessed' come from or mean?)

2) I was reading a statement that said "Data and the subroutines (called methods) that act on data are combined into a single entity called an object."

Did they mean ... subroutines - are called methods, or that ... data and the subroutines (together) are called methods? I was confused.


3) Can you explain ... in simple terms what a constructor is?? It said that a constructor is a special subroutine that defines a data type to maintain the attributes of a class and returns a reference to the variable.

Can you (in lay person's terms) explain this to me .. and maybe give me an easy to understand example??

4) Now, they also mentioned "attributes of a CLASS". Can you explain what a CLASS is ... and how it differs from an OBJECT ... and how that differs from a PACKAGE?? Please explain these - as they relate to each other - yet how they are different ... and maybe an example of how the work together, etc.

Thanks,
Gary

PS: If you know of an easy to understand (online) tutorial on the subject of object oriented programming (for a BEGINNER) would be very much appreciated.

Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
Hi Gary,

You're not the only one, notice how few questions refer to it here at TT. Perl OOP stuff is not widely regarded as the most elegant of OOP implementations; it is, however, quite usable and worth learning.

I'll have a go at a couple of your questions -- and others can then have fun pointing out my rather tenous grasp on Perl OOP ;-)

Most of this stuff is taken from Tom Christansen's excellent OOP documentation distributed with Perl in perltoot ("Tom's object-oriented tutorial for perl").

The bless() function enables its referent (the thing in brackets after the word bless) to be used as an object. Remember: being an object really means nothing more than that methods, functions, belonging to it may now be called against it. The bless() function is called when an object is created -- in a constructor function.

An object is a thing, consisting of data that defines the thing, and of ways you manipulate and retrieve that data (methods, that is).

I know, I know; that sounds sort of obvious. When I first read that description I thought "And your point is?"

The point is this: With OOP the only way of manipulating the data is with the methods supplied -- so adding, deleting, modifying data always happens using the predefined methods, predictably in other words. It will not depend on how well some other programmer understands the data structures, it should always be correct.

A class -- is a template, it's not an object itself but contains instructions on how to create (construct), modify and delete the objects it defines.

An Example (again, (mostly) from the Perl documentation). The following code should be in a file called "People.pm".
[tt]
use Person;

$a_person = People->new;
$a_person->name("Jason");
$a_person->age(23);
$a_perdon->peers( "Norbert", "Rhys", "Phineas" );
[/tt]

The fragment of code above creates a new object from the Person class. It then sets this person's name, his age and names some of his peers.

Here is the People package that defines the People class
[tt]
package People;
use strict;
#
# The constructor, creates an empty instance (object) of
# the People class.
#
sub new {
my $self = {};
$self->{NAME} = undef;
$self->{AGE} = undef;
$self->{PEERS} = [];
bless($self);
return $self;
}
#
# Methods: called with an argument (as in the example above)
# they set data in the object, without an argument they just
# return the current value of that bit of data (that
# attribute).
#
sub name {
my $self = shift;
if (@_) { $self->{NAME} = shift }
return $self->{NAME};
}
sub age {
my $self = shift;
if (@_) { $self->{AGE} = shift }
return $self->{AGE};
}
sub peers {
my $self = shift;
if (@_) { @{ $self->{PEERS} } = @_ }
return @{ $self->{PEERS} };
}
1; # so that "use People;" in a Perl program works
[/tt]

I hope that this is useful to you.

(I also hope that if anyone spots some glaring error of mine they will point it out -- bear in mind, though, that I deliberately haven't attempted to describe any advanced techniques here, the OP asked for a beginners tutorial, I couldn't find one online (not a really easy one that would have helped me) so thought I'd have a go at starting a basic OOP FAQ. So -- all those ommissions to sexy, advanced, georgously object oriented techniques .... are quite intentional <smile>) Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
That was good, but I being that I am very new at learning Perl, a lot of it (that might seem easy enough for someone who knows what is going on) ... is still confusing to me. I apologize for my lack of knowledge at this point, but I'd also appreciate any further and simplified explanation of OOP from you OR ... anyone else.

But, ... I do thank you. I did understand some of it, but I'll also have to read and re-read it a few times to see if I can make more sense out of some of the things that are still confusing.

Thanks again,
Gary

Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
The point of OOP is to have, if you will, a standard for data. That is, your object is the specification for how the data will behave. You hide all of the internals of the data, and provide public interfaces to make the data behave how you define it to. This does several things:

1. You (or someone else who uses it) doesen't have to know, or care, how it works or exactly what it does to use it, you just have to know what to use.

2. You can change the implementation without changing the interface. So what? you say, this is very powerful, because you can upgrade the internals of a class (the type of an object) and not even have to change any code that depends on it.

This is the gist of it, maybe folks will add where I left it thin.

MWB.

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Here, i'll do an example in non-programming terms.
Let's consider a lightbulb. A 120 watt lightbulb, manufactured by General Electric. A lightbulb is an object. It has various properties, such as being 120 watts, being manufactured by GE, having its particular shape, and having been used for 400 hours already. It also has various methods that users can invoke to make the lightbulb do things. You can screw it in, unscrew it, add electricity to it and make it shine, stop adding electricity to it.
In the lightbulb, there are various things that make it function as it should when the methods are called. it is shaped so that it screws in correctly, and it has wires and a filament so that it will shine when electricity is applied to it. but the user needn't ever see or understand these things, they simply have to follow its instructions and it will work.
Another way to look at this particular lightbulb is it is a particular kind of lightbulb, and shares many of the same properties and methods as all the other lightbulbs in the world. So, in this way, you can make a more general class of objects called &quot;lightbulbs&quot;. A class is not an object, but objects can be built off of it, using the same methods, but sometimes slightly differently.
In perl, part of the way to implement object oriented programming requires that each class be defined as a package. Package &quot;lightbulb&quot; would be the superclass, and package &quot;lightbulb_120_watt&quot; would be one of it's subclasses. When properly initiated, the subclass won't have to define all it's own methods, it can simply rely on the methods defined in the superclass.
Well, we're getting off the point.
A constructor is a method designed to create a new object of a particular type of class. A blessed reference is basically something that, in addition to being a scalar or whatever, is also an object that inherits all the methods and properties defined in the class it is made from, as well as all of that classes superclasses (usually). The basic formula for a contructor is something like this:[tt]
sub new {
my $proto = shift;
my $class = $proto || ref $proto;
my $self = {};
return (bless $self, $class);
}[/tt]
$self will then identify itself as an object of $class class. you'll then be able to call all of the methods defined in the class using the returned value as the caller. $self should be a reference, or just a scalar, but that means it could be a reference to anything. $self is where all the attributes of an object must be kept, so with any object with more than one attribute, it usually ends up being some sort of reference, often hashes work the best, as you can then easily name all of the attributes with keys, the values of which are then the values of the attributes(as was the case in Mike Lacey's example). then, whenever a method is called, it has to be called like in Mike's example, $obj->method(params). when it is done like this, the method receives as its first argument the actual object in question. then, whenever it accesses attributes, it will only be from that particular object. this allows for there to be multiple instances of objects from the same class, which all behave the same, but retain their own set of attributes.
Uh, i can't think of anything else at the moment, not that there's no more information to be said, and i've probly made mistakes in this post already (that's other people's cue to add more...). If you want to learn lots about object oriented programming, learn the Ruby language and you're guaranteed to learn enough OOP to easily get you through all this stuff. tek-tips has a forum on Ruby, if you're interested. it's fairly similar to perl, and i would put the time it would take to learn it at less than a month once you're familiar with perl.
HTH &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
oh, one mistake, $self HAS to be a reference. it can't just be a scalar value. sorry. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
You are awesome!!!

Thanks! :))

I appreciate the time it took to write that out for me.

Thank you!!

Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top