Chas Owens wrote: > On 6/22/07, Mathew Snyder <[EMAIL PROTECTED]> wrote: > snip >> I'm not sure what you mean by "Change your new() in a new() and init()". > snip >> What do you mean by "set/get approach"? > snip > > Some people believe that new should just create a new blank object and > call an init method to do setup. There are good arguments both ways. > > Manually writing Accessor, Mutator, Getter, or Setter methods sucks > and is error prone. It is often better to write one getter and one > setter that gets or sets the field(s) passed to it. Another method is > to use Perl's autoload capability to magically create subroutines for > you. Starting with some Perl in the 5.8 line the autoload function > gained the ability to be an lvalue, so I have written it that way. > > Perl script: > #!/usr/bin/perl > > use strict; > use warnings; > use Report; > > my $rpt = Report->new; > > $rpt->set({id => 5, queue => 10}); > print $rpt->printable; > > my ($id, $queue) = $rpt->get(qw(id queue)); > > print "id is $id and queue is $queue\n"; > > #same thing, but I like the => better > $rpt->set("id", 6); > $rpt->set(id => 6); > > print "id is now ", $rpt->get("id"), "\n"; > > $rpt->id = 7; > print "id is now ", $rpt->id, "\n"; > > Module: > package Report; > > use strict; > use warnings; > use Carp; > > our $AUTOLOAD; > our %fields = ( > _id => 1, > _queue => 1, > _owner => 1, > _priority => 1, > _worked => 1, > _timeLeft => 1, > _due => 1, > _created => 1, > _updated => 1, > _severity => 1, > _ccl => 1, > ); > > #minimal new > sub new { > my $class = shift; > my $self = bless {}, $class; > $self->init(@_); > return $self; > } > > #real object creation happens here > sub init { > my $self = shift; > my @fields = keys %fields; > @[EMAIL PROTECTED] = (undef) x @fields; > } > > sub _validate_field { > my ($self, $k) = @_; > croak "$k is not a valid field for " . ref $self > unless $fields{"_$k"}; > } > > #Getter/setter method 1 > sub get { > my ($self, @k) = @_; > my @ret; > for my $k (@k) { > $self->_validate_field($k); > push @ret, $self->{"_$k"}; > } > local $" = ' ::: '; > return @ret > } > > sub set { > my $self = shift; > croak "bad number of arguments" unless @_ == 2 or @_ == 1; > if (@_ == 2) { > $self->_validate_field($_[0]); > return $self->{"_$_[0]"} = $_[1]; > } > croak "not a hash reference" unless ref $_[0] eq 'HASH'; > my $h = $_[0]; > my @ret; > for my $k (keys %$h) { > $self->_validate_field($k); > push @ret, $self->{"_$k"} = $h->{$k}; > } > return @ret; > } > > #another form of setter/getter > > sub AUTOLOAD : lvalue { > my ($k) = $AUTOLOAD =~ /::(.*?)$/; > return if $k eq 'DESTROY'; > my $self = shift; > $self->_validate_field($k); > $self->{"_$k"}; > } > > sub printable { > my ($self) = @_; > > # return Printable Report info > return $self->id . " " . $self->queue . "\n"; > } > > 1; >
I pretty much have a very small idea of what is going on up there. Mathew Keep up with me and what I'm up to: http://theillien.blogspot.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/