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

Hilmar Lapp hlapp at gnf.org
Mon Dec 22 22:58:50 EST 2003


What if you overrode a method in this way? Would you still be able to 
call $obj->SUPER::overridden_method() or would you have messed up the 
inherited symbol table?

	-hilmar

On Monday, December 22, 2003, at 06:40  PM, Juguang Xiao wrote:

>
> 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";
>
> _______________________________________________
> Bioperl-l mailing list
> Bioperl-l at portal.open-bio.org
> http://portal.open-bio.org/mailman/listinfo/bioperl-l
>
>
-- 
-------------------------------------------------------------
Hilmar Lapp                            email: lapp at gnf.org
GNF, San Diego, Ca. 92121              phone: +1-858-812-1757
-------------------------------------------------------------




More information about the Bioperl-l mailing list