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
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