Re: accessor problem in OO





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



Relevant Pages

  • HP3000 spoolfile to PDF conversion tool *URGENT*
    ... Do You Yahoo!? ... Mail has the best spam protection around ... To join/leave the list, search archives, change list settings, * ...
    (comp.sys.hp.mpe)
  • Re: k3b - unsupported format
    ... attempting to burn the file to a cd using k3b. ... which does not support mp3 format. ... Do You Yahoo!? ... Mail has the best spam protection around ...
    (Fedora)
  • Re: Trying to add hyperlink
    ... ILO Links"); ... have typo if they follow the format above. ... Do You Yahoo!? ... Mail has the best spam protection around ...
    (perl.beginners)
  • Re: Telecom Digest archives questions [TELECOM]
    ... I find mbox to be a very useful format. ... One of my major frustrations with Yahoo groups is the lack of any bulk ... download capability, or any way of importing group archives into a ...
    (comp.dcom.telecom)
  • Re: reliability problem with Finance::QuoteHist::Yahoo
    ... Sometimes it arises when quotes() is executed, and at other times, it ... Without even looking at your code my guess is that Yahoo! ... reason that free services change their page format is to block people from ... if an individual trader needs a reliable and accurate ...
    (comp.lang.perl.misc)