Paso 3: Construcción del módulo
Así que vamos a ver lo que hemos aprendido en el modelo simple y combinarlo con la lista de métodos para crear un módulo reutilizable. El archivo pairwise.pm contiene el código ejecutable.
La estructura de un módulo es como sigue:
cat PACKAGENAME.pm #!/usr/bin/perl use strict;<br>package PACKAGENAME; <all that module code> 1; # optionally # documentation # commented out test cases
Vamos a encarnar los métodos del módulo descritos en el paso 1.
métodos de #initialization y miembro
sub new { my $self = {}; $self->{'map'} = {}; #empty hash $self->{'sort'} = 'dnc'; #sort order $self->{'sep'} = ','; #separator character default comma $self->{'debug'} = ''; #debug tokens bless($self); return $self; }
sub debug { my ( $self, $val ) = $self->{'debug'} = $val if defined($val); return $self->{'debug'}; }
sub reset { my ( $self, $val ) = $self->{'map'} = {}; return ; }
#Helper method to override separator for map2str sub separator { my ( $self, $val ) = $self->{'sep'} = $val if defined($val); return $self->{'sep'}; }
# Imprimir métodos
sub map2str { my ($self ) = my $str = ''; my $hashref = $self->{'map'}; foreach my $key (keys(%$hashref)){ #print "key=$key\n"; foreach my $val (keys(%{$hashref->{$key}})){ # line format= key,cnt,val\n $str .= $key; $str .= $self->{'sep'}; $str .= $self->{map}->{$key}->{$val}; $str .= $self->{'sep'}; $str .= $val; $str .= "\n"; } } if ($self->{'debug'} =~ m/map2str/) { $str .= "DEBUG" . "\n"; $str .= Dumper $self->{'map'}; } return $str; }
# Los métodos de manipulación Asociación
#Add Key Value Pair sub addkvp { my ($self , $key , $val) = my $res = '1'; if ( defined($val) && defined($key)) { $self->{'map'}->{$key}->{$val} ++ ; } else { $self->{'map'}->{$key}->{$val} = '1'; $res = '0'; } return $res; }
#assert the whole map if you already have itsub assertkvps { my ($self , $map) = my $res = '1'; if ( defined($map) ) { $self->{'map'} = $map ; } else { $res = '0'; } return $res; }
#Remove Key Value Pair sub removekey { my ($self, $key ) = my $is_reg = 0 ; if (exists $self->{map}->{$key}) { $is_reg = 1 ; delete $self->{map}->{$key}; } return $is_reg; }
#Invert Key Value Pair<br>sub invertkvp { my ($self) = my $hashref = $self->{map}; my $revhash = {}; foreach my $key (keys(%$hashref)){ foreach my $val (keys(%{$hashref->{$key}})){ $revhash->{$val}->{$key} = $hashref->{$key}->{$val}; } } $self->{map} = $revhash; return; }
#Association métodos de evaluación:
# Does Key exist returns 0/2 Boolean based on key's existance sub is_registered { my ($self, $key ) = my $is_reg = 0 ; if (exists $self->{map}->{$key}) { $is_reg = 1 ; } return $is_reg; }
# List All Keys returns arrayref sub getkeys { my ($self, $key ) = #my my = keys(%{ $self->{map} }); return \ }
# get key value returns arrayref sub getvalue { my ($self, $key ) = my if (exists $self->{'map'}->{$key}) { = keys(%{ $self->{'map'}->{$key} }); } else { print "no\n";} return \ } <br>
#get the map back as a hash sub gethash { my ($self) = my $reg = $self->{map}; return $reg; }
#non-uniq Key Value Pair returns hashref of keys with more then 1 value sub nonuniqkvp { my ($self) = my $hashref = $self->{map}; my $nonuniq = {}; foreach my $key (keys(%$hashref)){ foreach my $val (keys(%{$hashref->{$key}})){ if ($hashref->{$key}->{$val} gt 1) { $nonuniq->{$key}->{$val} = $hashref->{$key}->{$val}; } } } return $nonuniq; }
#non-uniq Key Value Pair returns a hashref of kvp with link count greater then 1 sub nonuniqkeys { my ($self) = my $hashref = $self->{map}; my $nonuniq = {}; foreach my $key (keys(%$hashref)){ if (scalar(keys(%{$hashref->{$key}}) gt '1')) { $nonuniq->{$key} = $hashref->{$key}; } } return $nonuniq; }
No te olvides de cerrar el paquete con "1";
1;
Siguiente vamos a ejercer el paquete con ejemplos de casos de prueba.