[Bioperl-l] some not-so-good perl practice in bioperl

Juguang Xiao juguang at tll.org.sg
Mon Dec 22 21:40:49 EST 2003


On Tuesday, December 23, 2003, at 12:46  am, Hilmar Lapp wrote:

>
> On Monday, December 22, 2003, at 06:57  AM, Lincoln Stein wrote:
>
>>  A big problem in using any autoloader-based accessor system
>> is that the UNIVERSAL::can() method will no longer work properly on
>> autoloaded methods
>
> Very true, thanks for pointing this out. bioperl-db would immediately 
> and completely break, for instance, as it relies entirely on can() for 
> wrapping objects with a persistence shell.

Sorry, I may mislead you to remember the AUTOLOAD again, and should 
come with an example. This time I mean the methodology exampled below, 
the package Person.

I am using perl 5.8, on Mac OSX 10.2. UNIVERSAL::can is able to detect 
the accessor functions in this way, since this tip makes the truly and 
individual perl methods in symbol table, at compile time.

Regards,
Juguang

###############
package Person;

sub new {
     my $invocant = shift;
     my $self = bless({}, ref $invocant || $invocant);
     $self->init();
     return $self;
}

sub init {
     my $self = shift;
     $self->name("unnamed");
     $self->race("unknown");
     $self->aliases([]);
}

for my $field (qw(name race aliases)) {
     my $slot = __PACKAGE__ . "::$field";
     no strict "refs";          # So symbolic ref to typeglob works.

     *$field = sub {
         my $self = shift;
         $self->{$slot} = shift if @_;
         return $self->{$slot};
     };
}

package main;
use Person;
my $he=Person->new();
print ($he->can('name')?'Y':'N'), "\n";



More information about the Bioperl-l mailing list