On 18 Jul 2001 [EMAIL PROTECTED] wrote:

> I am doing some Object Oriented programming in Perl.
>
> #my class constructor
> sub new {
>
> my $invocant = shift;
> my $class = ref($invocant) || $invocant;
> my $self = [];
> $self ->[ACCESS_METHOD] = undef;
> $self ->[TOTAL_ACCESS_TIME] = undef;
> $self ->[HOURLY_ACCESSES] = [23];
>
> return bless $self, $class;
> }

First of all, why are you using an array here when you are still
accessing things by name?  You should use a hash.

I would do something like:

our $AUTOLOAD;

sub new {

  my $class = shift;
  my $self = {
                access_method => undef,
                total_access_time => undef,
                hourly_access => 23
             };
  return bless $self, $class;
}

And then you can create access methods with AUTOLOAD:

sub AUTOLOAD {

  my $self = shift;
  my $class = ref($self) or die "$self is not an object\n";

  my $attrib = $AUTOLOAD;
  $attrib =~ s/.*://;

  unless(exists $self->{$attrib}) {
    die "$attrib does not exist in class $class\n";
  }

  if(@_) { return $self->{$attrib} = shift }
  else { return $self->{$attrib} }
}

Now all of your accessor (get and put) are generated on the fly.

-- Brett
                                   http://www.chapelperilous.net/btfwk/
------------------------------------------------------------------------
As he had feared, his orders had been forgotten and everyone had brought
the potato salad.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to