Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Learning OOP

Reply
Thread Tools

Learning OOP

 
 
Nene
Guest
Posts: n/a
 
      06-15-2012
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
 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      06-15-2012
In article <(E-Mail Removed)>,
Nene <(E-Mail Removed)> wrote:

> 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();

--
Jim Gibson
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Learning Perl CGI and OOP, not printing @c Nene Perl Misc 3 10-21-2011 08:03 PM
Learning C and Learning Make/Configure/Building/Linking Hal Vaughan C Programming 7 03-21-2006 05:07 PM
e-learning, (collaborative learning environment) collinm Java 1 09-08-2005 09:52 PM
Newbie learning OOP LenS Python 5 06-01-2005 06:02 PM
Newbie learning OOP 2nd ? LenS Python 2 05-30-2005 01:34 PM



Advertisments