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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OOP: Object definition and constuctor. 2

Status
Not open for further replies.

garymgordon

Programmer
Apr 5, 2000
307
US
Here is the code I am trying to understand.

package Employee;
1;

sub new
{
my $object = {};
return bless $object;
}

If you can, please explain the following:


1) They say a constructor must be named "new" . Can you explain why and how this works. They say that the "new" subroutine defines a hash class object, but I don't really understand what they mean.

Does it have to named "new" ? Or could it be "new" plus another name like:

sub new nameOfSubroutine instead of just sub new


2) What is happening with:

RE: my $object = {};

for example, what is "my" doing?
what is $object ? Can this be named something else ... and what purpose is $object serving?
What is the { } ?? could it be anything else. (Please explain my options at this point.)


RE: return bless $object;

What is "bless" and how is it being used here?
What is being returned from "$object" ?
Where is the "return" being returned to??


One more question at this time.

They also say that the "first element of the array (element 0) contains the name of the class. In this case the string "Employee".

What do they mean by the above. I don't understand the how and why that the first element is the name of the class.

Plus, I don't understand how these two following subroutines are working together. (Maybe they aren't) But, I am just confused.

Your help would be greatly appreciated.


Here is the example they gave.

package Employee;
1;

sub new
{
my $object = {};
$object -> {"name"} = $_[1];
$object -> {"ssn"} = $_[2];
$object -> {"job_classification"} = $_[3];
$object -> {"salary"} = $_[4];

return bless $object;
}

sub calculateBonus
{
my $self = shift;
if ($self -> {"job_classification"} =~ m/Manager/)
{
return ($self -> {"salary"} * 0.10);
}

elsif ($self -> {"job_classification"} =~ m/Peon/)
{
return ($self -> {"salary"} * 0.05);
}
}



Thanks,
Gary



Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
I am really confused with regard to the last part of my question when they also the add to the package Employee;
the following.

$bobSmith = new Employee("Bob Smith", "123-45-6789", "Manager", 125000);

print $bobSmith->calculateBonus(),"\n";

How is the values that are being passed to the subroutine calculateBonus ... knowing how to find {"job_classification"} in the subroutine called new ???

Confused as to how it knows about the new subroutine ... and how the flow of this is going.

Thanks,
Gary
Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
You don't seem to be very familiar with perl.
However I'll try to give you an explanation.

A Perl Object is really just a hash (%) variable residing in a special namespace.
The "return bless $object;" does the namespace association, and the lines above

my $object = {};
$object -> {"name"} = $_[1];
$object -> {"ssn"} = $_[2];
$object -> {"job_classification"} = $_[3];
$object -> {"salary"} = $_[4];

Are ordinary assignments to a hash reference.
So what you get when you're assigning:

$bobSmith = new Employee("Bob Smith", "123-45-6789", "Manager", 125000);

is a reference to a hash with a namespace associated to the Employee package.
The namespace part plus an extended perl syntax opens up for calling conventions like

print $bobSmith->calculateBonus(),"\n";

Which can be read as calculateBones($bobSmith) but search the namespace of $bobSmith for the calculateBonus function.

The magic part of the calculateBonus function being able to find the values of the object variables is not magic at all. As you can see, your object is really a hash reference, carrying around all the values.

My best advice for yo is to learn ordinary perl first. Become familiar with basic perl ideas, and then move on to object oriented Perl.

Cheers.

Søren
 
Yes, I am new to learning Perl, but I need to understand this better at time.

Your help is greatly appreciated.

Could you try and explain the answers to my questions (that I wrote above).

If you could take my questions one at a time (from above), and try to answer them - in a simplified type of explantion, that would be SOOOOO much appreciated.

Thanks,
Gary


Gary M. Gordon, LLC
webmaster@garymgordon.com
Certified Web Developer ::
Application Programmer
 
1) They say a constructor must be named "new" . Can you explain why and how this works.

As is with perl, you get a lot of slack that often results in you shooting yourself in the foot. Constructors in c/c++ have to be named new, they don't in perl, but you should. Anything that blesses an object and returns it can be considered a constructor, but you should just name your constructor new and accept it. This makes it easier for other who want to understand your code, and it allows you to conform to the general practice.

for example, what is "my" doing?

my is a specail operator for declaring variables. It means that they go out of scope when the function that they are declared in ends. So:

sub foo{
my $bar;
$bar = 5;
}

foo;
print $bar

$bar is null, as it was declared with my and once the sub exits, $bar is gone.
Conversely:
sub foo{
$bar;
$bar = 5;
}

foo;
print $bar;

Will print 5. (no my = still in scope)

Bless is kind of a way of changing the type of an object. When you bless it, it officialy becomes part of the class. That is, you set up the hashref for the class, but it's not really in the class until you bless it.

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top