[Bioperl-l] do not hate individual getter/setters coding?
Heikki Lehvaslaiho
heikki@ebi.ac.uk
Mon Jan 20 14:50:34 EST 2003
Here comes an example of really complicated perl programming. At some
point, I did not have the bioperl emacs macros installed, so I "wrote"
this:
############### bioperl_method_template.pl ######################
#!/usr/bin/perl
$a = shift;
$a||= "FOO";
# the code on the first line should go into the standard new()
print "
\$$a && \$self->$a(\$$a);
=head2 $a
Title : $a
Usage : \$obj->$a();
Function:
Sets and returns the $a of the resource
Example :
Returns : string
Args : string
=cut
sub $a {
my (\$self,\$value) = \@_;
if( defined \$value) {
\$self->{'_$a'} = \$value;
}
return \$self->{'_$a'};
}
";
#################################################################
Enjoy,
-Heikki
On Mon, 2003-01-20 at 14:08, Jason Stajich wrote:
> This discussion has been had.
>
> See the following for example and read the whole set of threads.
> http://bioperl.org/pipermail/bioperl-l/2002-November/010208.html
>
> You are welcome to implement your code with AUTOLOAD (in fact you'll see
> the bioperl-run objects often support this), but many of us have our
> reasons for not wanting to use it all the time.
>
> Most importantly, get/set methods should be implemented properly
> allowing resetting of the value to undef as outlined in one of the posts
> and now as provided in the bioperl.lisp emacs template for code
> generation.
>
> Best,
> -jason
> On Wed, 15 Jan 2003, Xiao Juguang wrote:
>
> > Hi guys,
> >
> > I may be a very lazy coder, so that I was tired on writing tedious
> > getter/setters fragment, as well, as a source code reader, I felt bored
> > to read individule getter/setters occupying, sometimes, more than half
> > of module. Do you? I thought bioperl follows Perl's neat soul and Perl
> > hopes its features to be fully used. Should we just suggest to add 2
> > subs in Bio::Root::Root ? _autoload_methods and AUTOLOAD . They look
> > like this.
> >
> >
> > =head2 _autoload_methods
> >
> > This subroutine is usually invoked at the very beginning line of constructor,
> > to set subroutine names for getter and setters.
> >
> > SYNOPSIS
> >
> > sub new{
> > my ($class, @args) = @_;
> > my $self = $class->SUPER::new(@args);
> >
> > $self->_autoload_methods([qw(dbhost dbname dbuser dbpass)]); # Don't add if unnecessary.
> >
> > # Then, say
> >
> > my ($dbhost) = $self->_rearrange([qw(DBHOST)], @args);
> > $self->dbhost($dbhost);
> >
> > return $self;
> > }
> >
> > =cut
> >
> > sub _autoload_methods {
> > my ($self, $arg) = @_;
> > if(defined $arg && ref($arg) eq 'ARRAY'){
> > # $self->{_autoload_methods} = $arg;
> > push @{$self->{_autoload_methods}}, @{$arg};
> >
> > # equally explicitly declare the subs !!!
> > use subs @{$self->{_autoload_methods}};
> > }
> > return $self->{_autoload_methods};
> > }
> >
> > sub AUTOLOAD{
> > return if our $AUTOLOAD =~ /::DESTROY$/;
> > my ($self, $arg) = @_;
> > my $field = $AUTOLOAD;
> > $field =~ /::([\w\d]+)$/;
> > if($self->_autoload_methods && grep /$1/, @{$self->_autoload_methods}){
> > $self->{$field} = $arg if defined $arg;
> > return $self->{$field};
> > }else{
> > $self->throw("Can't find the method '$field'");
> > }
> > }
> >
> >
> > I remembered in this mailing list, there was a discussion on it. However, I forgot the harm to use it that people mention. In theory, Bio::Root::Root::AUTOLOAD will be almost last visited in the path of method searching. I just quote paragraphs from Programming Perl 3rd Ed. by Larry Wall, for your information.
> >
> > When your invoke a method 'methname' on an invocant of type 'classname', Perl tries six different ways to find a subroutine to use:
> > 1. First, Perl looks in the invocant's own package for a subroutine named classname::methname. If that fails, inheritance kicks in, and we go to step 2.
> > 2. Next, Perl checks for methods inherited from base classes by looking in all the parent packages listed in @classmane::ISA for a parent::methname subroutine. The search is left-to-right, recurive, and depth-first. The recursion assures that grandparent classes, great-grandparent classes, great-great-grandparent classes, and so on, are all searched.
> > 3. If that fails, Perl looks for a subroutine named UNIVERSAL::methname.
> > 4. At this point, Perl gives up on methname and starts looking for an AUTOLOAD. First, it looks for a subroutine named classname::AUTOLOAD.
> > 5. Failing that, Perl searches all parent packages listed in @classname::ISA, for any parent::AUTOLOAD subroutine. The search is again left-to-right, recurive, and depth-first.
> > 6. Finally, Perl looks for a subroutine named UNIVERSAL::AUTOLOAD.
> > Perl stops after the first successful attempt and invokes that subroutine.
> >
> > To make perldoc friendly, developer should write pod for each 'virtual getter/setter' or might write a =head2 item for a group of get/setters. It is also Perl itself friendly. I mean, since we 'use subs @{$self->{_autoload_methods}};', Perl regards them 'virtual' subs as real ones.
> >
> > My $0.0000002 worth.
> >
> > Please do correct me or my code. Thanks.
> >
> > Juguang.
> >
> >
> > _______________________________________________
> > Bioperl-l mailing list
> > Bioperl-l@bioperl.org
> > http://bioperl.org/mailman/listinfo/bioperl-l
> >
>
> --
> Jason Stajich
> Duke University
> jason at cgt.mc.duke.edu
> _______________________________________________
> Bioperl-l mailing list
> Bioperl-l@bioperl.org
> http://bioperl.org/mailman/listinfo/bioperl-l
--
______ _/ _/_____________________________________________________
_/ _/ http://www.ebi.ac.uk/mutations/
_/ _/ _/ Heikki Lehvaslaiho heikki@ebi.ac.uk
_/_/_/_/_/ EMBL Outstation, European Bioinformatics Institute
_/ _/ _/ Wellcome Trust Genome Campus, Hinxton
_/ _/ _/ Cambs. CB10 1SD, United Kingdom
_/ Phone: +44 (0)1223 494 644 FAX: +44 (0)1223 494 468
___ _/_/_/_/_/________________________________________________________
More information about the Bioperl-l
mailing list