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

Juguang Xiao juguang at tll.org.sg
Tue Dec 23 02:00:04 EST 2003


On Tuesday, December 23, 2003, at 11:58  am, Hilmar Lapp wrote:

> 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?
>

Good question. I am recently hacking the Perl symbol table and able to 
answer this, hopefully correctly. :-)

$obj->SUPER::overridden_method() means SUPER::overriden($obj), which 
will look up the symbol table of SUPER, say Parent, the most left 
module in @ISA of this $obj module, and find the code entry that is 
\&{Parent::overriden_method}, and pass $obj as the first argument.

*pkg::sym{SCALAR}      # same as \$pkg::sym
*pkg::sym{ARRAY}       # same as \@pkg::sym
*pkg::sym{HASH}        # same as \%pkg::sym
*pkg::sym{CODE}        # same as \&pkg::sym

>>
>> 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};
>>     };

this does the same as your hard source code, say, sub name.

*{Person::name} = sub {...};

also puzzlingly same as

*{main::Person::name}= sub {...};

The either sub or super class of Person will have different symbol key 
prefix, because what Perl looks at is which package a method is defined 
in. If the Employee inherits Person, and you have the code like,

package Employee;
our @ISA=qw(Person); # this line does not affect the symbol table when 
we search 'name'

*name = sub {...}; # the consequence will be adding a 
*Employee::name{CODE} into Employee symbol table.

1;

The symbol table is constructed like, as my understanding,

{
	Employee::name => {
		CODE => \sub {},
	},
	Employee::ISA => {
		ARRAY => [Person], # defined as our @ISA, can be use as 
@{Employee::ISA}
	},
	Person::name => {
		CODE => \sub {}
	}
}

Well, we start to dive more hacky. We all should read Programming Perl 
Chapter 10..12 again, if we want to go on. Let me know if your 
understanding is different from mine. Thanks. Again, you are at your 
own ease and risk.

Juguang

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



More information about the Bioperl-l mailing list