Stap 3: Bouw van de module
Dus laten we zien wat we geleerd hebben in het simpele model en combineer het met de lijst van de methoden voor het maken van een herbruikbare module. Het pairwise.pm bestand als bijlage bevat de uitvoerbare code.
De structuur van een module is als volgt:
cat PACKAGENAME.pm #!/usr/bin/perl use strict;<br>package PACKAGENAME; <all that module code> 1; # optionally # documentation # commented out test cases
Laten we vlees uit de methoden van de module beschreven in stap 1.
#initialization & lid methoden
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'}; }
# Print methoden
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; }
# Vereniging manipulatie methoden
#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 evaluatiemethoden:
# 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; }
Vergeet niet te sluiten uit het pakket met "1;"
1;
Volgende laten we oefenen het pakket met testcase voorbeelden.