sub foo
{
  my $self = shift; #object

  if (@_ == 0) #any other arguments?
  {
        return $self->{'foo'}; #getter (no other arguments)
  }

   return $self->{'foo'} = shift; # setter (save next argument)

}

On 5/27/06, Peter Cornelius <[EMAIL PROTECTED]> 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

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>





--
Anthony Ettinger
Signature: http://chovy.dyndns.org/hcard.html

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to