package BurguillosInfo::Species; use v5.36.0; use strict; use warnings; use Moo; use Module::Pluggable search_path => ['BurguillosInfo::Species'], instantiate => 'new', on_require_error => sub ( $plugin, $error ) { die $error; }; { my %hash_species; sub _hash ($self) { if ( !scalar keys %hash_species ) { $self->_populate_hash; } return {%hash_species}; } sub _populate_hash ($self) { my @species = $self->plugins(); print Data::Dumper::Dumper \@species; for my $specie (@species) { $self->_check_specie_valid($specie); if (exists $hash_species{$specie->id}) { die "Duplicated species id @{[$specie->id]}."; } $hash_species{$specie->id} = $specie; } } } sub _check_specie_valid ( $self, $specie ) { if ( !$specie->does('BurguillosInfo::Specie') ) { die "$specie does not implement BurguillosInfo::Specie."; } } sub get($self, $id) { return $self->_hash->{$id}; } sub list($self) { my @species_keys = keys %{$self->_hash}; my $species = [ sort { $a->id cmp $b->id } map { $self->_hash->{$_} } @species_keys ]; return $species; } sub list_can_be_global($self) { return [ grep { $_->can_be_global } $self->list->@* ]; } 1;