sub parse { if ( $_[0] and $_[0] eq __PACKAGE__ or ref $_[0] eq __PACKAGE__ ) { shift; } my %opts; if ( ! @_ || scalar @_ % 2 ) { Bio::Phylo::Exceptions::OddHash->throw( error => 'Odd number of elements in hash assignment' ); } eval { %opts = @_; }; if ( $@ ) { Bio::Phylo::Exceptions::OddHash->throw( error => $@ ); } if ( ! $opts{'-format'} ) { Bio::Phylo::Exceptions::BadArgs->throw( error => 'no format specified.' ); } if ( ! grep ucfirst $opts{'-format'}, @parsers ) { Bio::Phylo::Exceptions::BadFormat->throw( error => 'no parser available for specified format.' ); } if ( ! $opts{'-file'} && ! $opts{'-string'} ) { Bio::Phylo::Exceptions::BadArgs->throw( error => 'no parseable data source specified.' ); } my $lib = 'Bio::Phylo::Parsers::' . ucfirst $opts{-format}; eval "require $lib"; if ( $@ ) { Bio::Phylo::Exceptions::ExtensionError->throw( error => $@ ); } my $parser = $lib->_new; if ( $opts{-file} && $parser->can('_from_handle') ) { eval { open FH, '<', $opts{-file}; }; if ( $! ) { Bio::Phylo::Exceptions::FileError->throw( error => $! ); } $opts{-handle} = *FH; return $parser->_from_handle(%opts); } elsif ( $opts{-string} && $parser->can('_from_string') ) { return $parser->_from_string(%opts); } elsif ( $opts{-string} && ! $parser->can('_from_string') ) { Bio::Phylo::Exceptions::BadArgs->throw( error => "$opts{-format} parser can't handle strings" ); } }