[Bioperl-l] Auto-method caller proposal

Chris Fields cjfields at uiuc.edu
Tue Jan 9 11:19:17 EST 2007


On Jan 9, 2007, at 5:49 AM, Sendu Bala wrote:

> Chris Fields wrote:
>> The idea is one could add in customized code fairly easily using this
>> (very useful, BTW) method, or a similar one.  For example, I could
>> replace code defined in a dispatch table with my own on the fly by
>> passing in new code as an hash ref:
>>
>> # customized subs my %subs = ( 'ids' => '# modified id() for
>> customized ID retrieval, not std get/set', 'score'   => '# modified
>> score() code using myparam() and mydata()' );
>>
>> # customized user-params (get/sets) my @params = (qw(myparam
>> mydata));
>>
>> # pass into similar method to _set_from_args() # using parameters and
>> hash/array refs
>
> I'm not sure I understand. Can you offer a specific example of this  
> sort
> of thing being used?

There are a few examples in Higher Order Perl (and maybe Advanced  
Perl, but I can't remember).  These are really dispatch tables for  
config files.

>  From what I initially understood of your idea, in class  
> Bio::MyNewClass
> you write out a sub as a string and associate it with a key like
>
> 'bar' => 'my $self=shift; my $bar = $shift if @_; return
> ($self->foo)*$bar if $bar;'
>
> and in some way supply that pair to something such that when bar() is
> called by the user, the string is evaluated and used as the method. In
> what circumstance does this provide any benefit (to the author or the
> user) over defining the following sub explicitly in Bio::MyNewClass ?
>
> sub bar {
>   my $self=shift;
>   my $bar = $shift if @_;
>   return ($self->foo)*$bar if $bar;
> }

Don't read too much into my posted example.  I'm basically just using  
that to demonstrate what could be done w/o having to define a brand- 
new class or make modifications to the Bioperl code directly.

As I said before, there are much simpler ways to go about it  
(including your suggestion and the previous suggestions by Aaron and  
Nat using Class* methods), so it's probably more trouble than it's  
worth to even worry about.

>> BTW, don't know if this is possible, but can you get around 'no
>> strict "refs"' by building the sub code as a string and using an
>> eval?  I did something like this in the EUtilities BEGIN block using
>> heredoc, something picked up from Brian's Bio::DB::Query::GenBank.
>
> The reference in question is the method name, not the method code.

Now you pointed it out, yes, went back and looked again.  I was  
thinking of something like the heredoc version I used in the past:

     our @METHODS = qw(foo bar);
     for my $method (@METHODS) {
         eval <<END;
sub $method {
     my \$self = shift;
     return \$self->{'_$method'} = shift if \@_;
     return \$self->{'_$method'};
}
END


chris



More information about the Bioperl-l mailing list