You could also just have one method that is both a getter and setter

sub level {
        my $self = shift;

        if ( @_ ) { $self->{ Level } = shift;

        return $self->{ Level };
}

What we are doing is checking if any other arguments are passed, 
apart from the reference to the object it's self, if there are we just 
grab the next one.

> On Mon, 18 Jun 2001, Nick Transier wrote:
> 
> > Assuming I am defining an object which has as its only property a
> > level associated with it, would these be the correct simple
> > subroutines to retrieve and set that property. Also, is this the
> > correct usage of the @_ array such that the level value would be 999
> > unless I called new(Level => something else)
> >
> >
> > sub new {
> >
> >     my $invocant = shift;
> >     my $class = ref($invocant) || $invocant;
> >     my $self = {
> >             Level => 999,
> >             @_,
> >     };
> >     return bless $self,$class;
> >
> > }
> >
> > sub getlevel {
> >     return $$self{Level};
> > }
> 
> You need to remember to grab the $self argument passed to each
> argument:
> 
> sub getlevel {
>  my $self = shift;
>  return $self->{Level};
> }
> 
> > sub setlevel {
> >     $$self{Level} = shift(@_);
> > }
> 
> sub setlevel {
>  my $self = shift;
>  $self->{Level} = shift;
> }
> 
> -- Brett
> 
>        http://www.chapelperilous.net/btfwk/
> ----------------------------------------------------------------------
> -- To err is human, but when the eraser wears out before the pencil,
> you're overdoing it a little.
> 
> 





Martin van-Eerde Dip. Comp. (Open)
Senior Analyst Programmer
==========================================================

Reply via email to