use person;
my $foo = Person->new();
$foo->name("Stefan");
$foo->age(20);
$foo->exclaim();
and you also want to be able to do something like this,
$bar = Person->new();
$bar->name("Yoda");
$bar->age(900);
$bar->exclaim();
in the same program.
I think the conceptual gap you are having is, an object in perl is really just a reference. (In this case a reference to a hash.) Perl uses some syntactic sugar that makes object-oriented programing easier. (Confusing for the beginner, but more powerful for the wizard) When you use object-oriented syntax to make a “method call” (a function call via an object) using code that looks like this:
$foo->name("Stefan");
it is really the same as making a regular function call like this
Person::name( $foo , "Stefan” );
That is, if $foo is a reference that has been blessed into to a Person object.
You see, the blessed reference “knows” what package it belongs to. So the ‘Person::’ gets tacked on the front of the function call. Also, the reference is used as the first argument to the function. That’s why all the method functions shift off a reference from the argument list.
This method of shifting off implied references may seem strange but it is the key to having multiple objects from the same class. Consider the following:
my $ref1 = { “name” => “Stefan”,
“age” => 20 };
my $ref2 = { “name” => “Yoda”,
“age” => 900 };
These are obviously two separate anonymous hashes. Now if we were to do the following (preferably in new or any constructor subroutine)
$ref1 = bless $ref1 “Person”;
$ref2 = bless $ref2 “Person”;
we would have two completely separate Person objects.
The bless function takes a reference as the first argument, and a class name as the second argument. All it does is allow you to call functions that are in a package that is specified when the reference is blessed, but the package does not need to be specified when the function is called.
With this in mind, see if you understand the following. I tried to make it do what I though you wanted Person to do
package Person;
require 5.004;
#use strict;
my $Person;
# These Variables are Static variables.
# All Person objects share these variables.
$Person::version = "1.00.01";
$Person::build = "\$ID:2255518";
# this only lets you create one person. I don't think you
# whan to do that.
# $Person::list = {};
sub new
{
my $class = shift;
# $person is a blessed reference to an anonymous hash
my $Person = {};
my $Person = bless $Person, $class;
return $Person;
}
sub name
{
# Get the reference to the hash that represents this person
$Person = shift;
# Set the name member of the anonymous hash to the argument
# of this function.
$Person->{NAME} = shift;
return $Person->{NAME};
}
sub age
{
# Get the reference to the hash that represents this person
$Person = shift;
# set the age member of the anonymous hash to the argument
$Person->{AGE} = shift if @_;
return $Person->{AGE};
}
sub exclaim
{
# Get the reference to the hash that represents this person
$Person = shift;
printf "I'm %s and i'm %d years old!\n",
$Person->{NAME},$Person->{AGE};
return 1;
}
return 1;
The above code works when called as follows.
I kept your code in comments to correlate with your original question.
#!/usr/bin/perl
# use Person qw/name age exclaim/;
use person;
my $foo = Person->new();
# $foo->Person::name("Stefan");
$foo->name("Stefan");
# $foo->Person::name(20);
$foo->age(20);
# $foo->Person::exclaim();
$foo->exclaim();
$bar = Person->new();
$ bar->name("Yoda");
$ bar->age(900);
$ bar->exclaim();
# See, it's still here
$foo->exclaim();
I’m Sure the above could be optimized quite a bit. But I tried to make one step per line for educational purposes.
For more information I really recommend you read perltoot it should also be included with the perl documentation on your system. It contains far more than you ever wanted to know about objects in perl. I believe it also includes examples.
As an exercise see if you can modify new so that it can be called as follows
my $foo->new( “Stephan”, 20 );
$foo->exclaim();
my $bar->new( “Yoda”, 900 );
$bar->exclaim();
Can you tell I just saw Star Wars?
|