Anthony Ettinger am Samstag, 27. Mai 2006 22.02:
> 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)
>
> }

Hi together

This version could be written shorter (keeping the "shift $self" and premature 
return):

sub foo {
   my $self=shift;
   return $self->{foo} unless @_; # note the missing quotes (not necessary)
                                  # and the equality of [EMAIL PROTECTED] and 
@_==0
   $self->{foo}=$_[0];            # note the missing return; returns the
                                  # last evaluated expression, which is the
                                  # assigned value
   # or: $self->{foo}=shift;
}

The above versions, as version 1 and version 2 below return 
the new attribute value, if it's set. Also in use are mutators that return 
the _old_ one.

Chen Li, also have a look at the (source code of) Class::Accessor and 
Class::Accessor::Fast modules; they can be used for the simple cases that an 
attribute is only got/set without doing anything additional work. They create 
accessors/mutators automatically (avoiding repetitive code if there are a lot 
of a/m) - at the cost of a little loss of performance.

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

which is created in the new() class method in the line
my [EMAIL PROTECTED];  

(
my [EMAIL PROTECTED]; # create (and assign) hash reference
my [EMAIL PROTECTED]; # create (and assign) array reference
)

> > >    return $self->{_name}
> > >  }
> > >
> > >
> > > Another version for the subroutine name
> > >
> > > sub name{ #verstion 2
> > > $_[0]->{_name}=$_[1] if  defined }$_[1];
      v                                ^
> > > {$_[0]->{_name}
> > >                   }

There's a {} pair too much, leading to a syntax error

sub name {
   $_[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}

It not only gets it from the hashref object, it also returns this value since 
this (the dereferencing of the attribute value) is the last expression in the 
object method.

(Compare the 
1; 
at the end of perl modules: A module must return a true value to indicate that 
is has been loaded successfully. It's good practice, although only neccessary 
if the evaluated expression before this "1;" would give a false value)

> > > (I know  that the $_[0] in the default array @_ is
> > > actually an object $self. )


Dani

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