Learning OOP

N

Nene

I want to learn OOP and really understand it.

Below is a snip from the perldoc. A few questions; in the constructor, I see
my $self = {}; Am I creating an annonymous hash called $self?

$self->{NAME} = undef (Is $self the object? and I'm invoking a method called {NAME)? )

Can somebody explain in details the method:

sub name {
my $self = shift;
if (@_) { $self->{NAME} = shift }
return $self->{NAME};


###########################################

package Person;
use strict;

##################################################
## the object constructor (simplistic version) ##
##################################################
sub new {
my $self = {};
$self->{NAME} = undef;
$self->{AGE} = undef;
$self->{PEERS} = [];
bless($self); # but see below
return $self;
}

##############################################
## methods to access per-object data ##
## ##
## With args, they set the value. Without ##
## any, they only retrieve it/them. ##
##############################################

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 the require or use succeeds
 
J

Jim Gibson

Nene said:
I want to learn OOP and really understand it.

Below is a snip from the perldoc. A few questions; in the constructor, I see
my $self = {}; Am I creating an annonymous hash called $self?

You are creating an anonymous hash. Being anonymous, it does not have a
name.

You are then assigning a reference to that anonymous hash to the scalar
variable $self.
$self->{NAME} = undef (Is $self the object? and I'm invoking a method
called {NAME)? )

You are storing the (key,value) pair ('NAME',undef) in the anonymous
hash referred to by $self. No methods are being called.

$self is probably an object. We would need to see some more code to say
for sure. E.g., is $self ever 'blessed' into a class/package.
Can somebody explain in details the method:

sub name {
my $self = shift;
if (@_) { $self->{NAME} = shift }
return $self->{NAME};

The first value in the array @_, which is used to pass arguments to
subroutines, is shifted off and assigned to $self.

If any values remain in the array @_, the next one ($_[0]) is shifted
off the front of the array and stored in the hash value $self-{NAME}.

The value of $self-{NAME} is returned to the caller.

This is a pattern of method implementation that allows a single method
to be used both as a "getter" and a "setter":

# set name
$self->name("Fred");

# get name
my $name = $self->name();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top