Perl OO containment confusion
I seem to be a bit confused when it comes to object and class relationships.
I've read as many perldocs as i can and still i dont get it. My problems seem to revolve around things such as "use", @ISA, containment, and inheritance.
The example below uses clips of pertoot.
QUESTIONS.
1. In the constructor for the Person package (below), the key FULLNAME in the annonymous hash points to a reference for a new FULLNAME object. But when the new Person object is blessed, why isnt the FULLNAME object (created by the person constructor) blessed into the class PERSON. ??
2.In the TEST SCRIPT we have the line "use Person" so that Persons symbols appear in the scripts symbol table. BUT why isnt there a "use Fullname" line at the top of the Peron package so that the Person constructor has access to the Fullname constructor:
$self -> {FULLNAME} = Fullname->new();
3.What do the two "%s" mean on the last line of TEST SCRIPT (I dont know if you will need all the perltoot document to be able to tell me this).
I would be very grateful for any help (Ive been on this for a week now).
NOTE. This is a learning exercise. Im not looking for another way to do something, simply to understand the principles of containment and possibly inheritance.
TIA.
package Person;
sub new {
my $class = shift;
my $self = {};
$self -> {FULLNAME} = Fullname->new();
$self -> {AGE} = undef;
bless $self,$class;
return $self;
}
sub fullname {
my $self = shift;
return $self->{FULLNAME};
}
sub name {
my $self = shift;
return $self->{FULLNAME}->nickname(@_)
|| $self->{FULLNAME}->christian(@_);
}
sub age {
my $self = shift;
if (@_) {$self -> {AGE} = shift}
return $self->{AGE};
}
1:
package Fullname;
sub new {
my $class = shift;
my $self = {
TITLE => undef,
CHRISTIAN => undef,
SURNAME => undef,
NICKNAME => undef,
};
bless $self,$class;
return $self;
}
sub christian {
my $self = shift;
if (@_) {$self->{CHRISTIAN} = shift}
return $self->{CHRISTIAN};
}
1;
# TEST SCRIPT
use strict;
use Person;
my $him = Person->new();
$him->fullname->christian ("Thomas");
$him->fullname->surname ("Aquinas");
$him->age (1);
printf "%s is really %s. \n", $him->name, $him->fullname->as_string;
Abhishek Reddy posted this at 05:43 — 12th November 2006.
He has: 3,348 posts
Joined: Jul 2001
1. Why do you think the Fullname object should be blessed into Person? When bless is called in the Person constructor, $self refers to the hash containing FULLNAME and AGE, not the Fullname object itself.
2. You're right. Person.pm must call use Fullname;. If Perl tries compiling that without the use call, it should report an error about stuff not being found. The perltoot docs don't provide complete code.
3. %s represents a substitution flag. How it works is each %s is replaced by subsequent arguments to printf in order. E.g. printf "%s%s", "foo", "bar" will print foobar. In the test script, $him->name is evaluated and substituted at the first %s, and $him->fullname->as_string at the second %s.
It's effectively like printf $him->name . " is really " . $him->fullname->as_string . ". \n"; if that's what you're used to. See the printf doc for more.
StuPeas posted this at 09:48 — 12th November 2006.
They have: 36 posts
Joined: Oct 2004
Thanks alot Abhishek. One more question; what if the two packages were in the same file, would 'use' need to be used then ?
Abhishek Reddy posted this at 10:59 — 12th November 2006.
He has: 3,348 posts
Joined: Jul 2001
That'll work too -- no need for use if they're in the same file.
Whether you should group multiple packages/classes in a file is an interesting question. Some people dislike the Java tradition of putting every class in its own file. In this case, they might prefer to group Person and Fullname. For others, it may make more sense to separate them. Naturally, Perl accepts both approaches.
Personally, I'd separate them because I prefer to put each package in its own file, though not each class. I really don't like how packages are classes in Perl.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.