Skip to content
Browse files

Instead of closures, simply store attributes in $selft

  • Loading branch information...
1 parent ecab77f commit d4a13cdbdab73e5cb868d5a5f842c5cf936a58b0 @fangly fangly committed Nov 16, 2012
Showing with 16 additions and 42 deletions.
  1. +8 −36 Bio/DB/IndexedBase.pm
  2. +8 −6 t/LocalDB/Fasta.t
View
44 Bio/DB/IndexedBase.pm
@@ -268,32 +268,6 @@ use constant DIE_ON_MISSMATCHED_LINES => 1;
# you can avoid dying if you want but you may get incorrect results
-# Store file information, but:
-# * It cannot be stored as a $self object attribute because not all interfaces
-# of this module use an object-oriented interface.
-# * It can not be a my() or our() class variable either because it would clash
-# when running multiple indexed databases in the same script.
-# * It could be be stored with the file offsets in the indexed hash file but
-# mixing different types of data in the same hash is suboptimal.
-# A solution is to use a closure:
-
-my $data = {
- fileno2path => [] ,
- filepath2no => {} ,
-};
-
-my $closure = sub {
- my ($field, $val) = @_;
- if (not exists $data->{$field}) {
- die "Error: Invalid field $field\n";
- }
- if (defined $val) {
- $data->{$field} = $val;
- }
- return $data->{$field};
-};
-
-
=head2 new
Title : new
@@ -368,6 +342,8 @@ sub new {
index_name => $opts{-index_name},
obj_class => eval '$'.$class.'::obj_class',
offset_meth => \&{$class.'::_calculate_offsets'},
+ fileno2path => [],
+ filepath2no => {},
}, $class;
my ($offsets, $dirname);
@@ -980,22 +956,18 @@ sub file {
sub _fileno2path {
my ($self, $fileno) = @_;
- my $fileno2path = $closure->('fileno2path');
- return $fileno2path->[$fileno];
+ return $self->{fileno2path}->[$fileno];
}
sub _path2fileno {
my ($self, $path) = @_;
- my $filepath2no = $closure->('filepath2no');
- if ( not exists $filepath2no->{$path} ) {
- my $fileno = ($filepath2no->{$path} = 0+ $self->{fileno}++);
- # Save path
- my $fileno2path = $closure->('fileno2path');
- $fileno2path->[$fileno] = $path;
- $closure->('fileno2path', $fileno2path);
+ if ( not exists $self->{filepath2no}->{$path} ) {
+ my $fileno = ($self->{filepath2no}->{$path} = 0+ $self->{fileno}++);
+ $self->{fileno2path}->[$fileno] = $path; # Save path
}
- return $filepath2no->{$path};
+ return $self->{filepath2no}->{$path};
+
}
View
14 t/LocalDB/Fasta.t
@@ -2,7 +2,7 @@ BEGIN {
use lib '.';
use Bio::Root::Test;
- test_begin( -tests => 89,
+ test_begin( -tests => 91,
-requires_modules => [qw(Bio::DB::Fasta Bio::SeqIO)]);
}
use strict;
@@ -147,13 +147,15 @@ my $test_files = [
{
# Concurrent databases (bug #3390)
ok my $db1 = Bio::DB::Fasta->new( test_input_file('dbfa', '1.fa') );
+ ok my $db3 = Bio::DB::Fasta->new( test_input_file('dbfa', '3.fa') );
+ ok my $db4 = Bio::DB::Fasta->new( $test_dir );
ok my $db2 = Bio::DB::Fasta->new( test_input_file('dbfa', '2.fa') );
- ok my $db3 = Bio::DB::Fasta->new( $test_dir );
- is $db1->file('AW057231'), '1.fa';
+ is $db4->file('AW057231'), '1.fa';
is $db2->file('AW057302'), '2.fa';
- is $db3->file('AW057231'), '1.fa';
- is $db3->file('AW057119'), '1.fa';
- is $db3->file('AW057410'), '3.fa';
+ is $db4->file('AW057119'), '1.fa';
+ is $db3->file('AW057336'), '3.fa';
+ is $db1->file('AW057231'), '1.fa';
+ is $db4->file('AW057410'), '3.fa';
}

0 comments on commit d4a13cd

Please sign in to comment.
Something went wrong with that request. Please try again.