Re: accessor problem in OO
- From: chen_li3@xxxxxxxxx (Chen Li)
- Date: Sat, 27 May 2006 15:56:40 -0700 (PDT)
--- Peter Cornelius <peterc@xxxxxxxxxxxxxx> wrote:
This might be a little more clear if you break down
the way arguments
are being passed in and what they actually are. It
sounds like
you're aware that the arguments are passed in as a
list name @_, it
looks like the arguments are something like this:
@_ = ( #<---- A list
{ _name => 'something' }, # <---- a hash
reference (objects are
hashes in perl)
'some set name string' #<---- a string
);
The first version extracts the arguments so that you
can refer to
them by names that might have some meaning to
someone maintaining
your code latter on. The second version accesses
them directly.
sub name{ #version 1
my $self=shift;
shift is going to give you $_[0]
my $set_name=shift;
This sets $set_name to $_[1]
$self->{_name}=$set_name if defined $set_name;
So this is equivalent to
$_[0]->{_name} = $_[1] if defined $_[1]
the '->' is to dereference the hash reference stored
in $_[0].
return $self->{_name}
}
Another version for the subroutine name
sub name{ #verstion 2
$_[0]->{_name}=$_[1] if defined }$_[1];
{$_[0]->{_name}
}
I feel a little bit confuse about the verion 2
subroutine name. The way it gets the value of
attribute name looks like this to me:
array element->{_name}
(I know that the $_[0] in the default array @_ is
actually an object $self. )
Hope this helps
PC
Thank you all for the reply.
Based on what I learn the regular method to defer a
hash reference to get specific value takes this
format:
$ref_hash->{key1}
but in this line
$_[0]->{_name}= $_[1] if defined $_[1]
the format is
array element->{_name}
Is the middle man $ref_hash is omitted in this format?
Does this what Perl really sees:
$_[0]=$ref_hash;
$ref_hash->{_name};
and put these two lines into one line to make it
short:
$_[0]->{_name}
Li
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
.
- Follow-Ups:
- Re: accessor problem in OO
- From: Peter Cornelius
- Re: accessor problem in OO
- From: D. Bolliger
- Re: accessor problem in OO
- References:
- Re: accessor problem in OO
- From: Peter Cornelius
- Re: accessor problem in OO
- Prev by Date: Re: accessor problem in OO
- Next by Date: Re: accessor problem in OO
- Previous by thread: Re: accessor problem in OO
- Next by thread: Re: accessor problem in OO
- Index(es):
Relevant Pages
|