Algorithm-CheckDigits
view release on metacpan or search on metacpan
lib/Algorithm/CheckDigits.pm view on Meta::CPAN
use warnings;
use Carp;
use vars qw($AUTOLOAD);
require Exporter;
our @ISA = qw(Exporter);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use CheckDigits ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
our %EXPORT_TAGS = (
'all' => [
qw(
CheckDigits method_descriptions method_list print_methods
) ] );
lib/Algorithm/CheckDigits.pm view on Meta::CPAN
strategies like last one wins (the module that registers last for a given
handle is the one that is choosen) or first one wins (the first registered
module is choosen). Instead I went for something more complex to assure that
every module that wants to get registered will be registered and that every
registered module will be accessible by the same handle as long as the program
runs. To make this work C<plug_in()> sees the third argument only as a hint
how the handle should look like, when a module is registered. It returns the
real handle with which the algorithm can be instantiated. That means a
developer of a plugin module cannot make the handle immediately available like
I did for the modules in the distribution. Instead there should be something
like a public variable or function that returns the handle as it came back
from the C<plug_in()> function.
This could go like this in the module:
package Algorithm::XyZ;
use Algorithm::CheckDigits;
our $xyz = Algorithm::CheckDigits::plug_in('Algorithm::XyZ',
'XyZ check digits',
t/PluginLibA.pm view on Meta::CPAN
# This module inherits from Algorithm::CheckDigits end reexports the function
# CheckDigits() so the user of this module does not need to explicitely
# 'use Algorithm::CheckDigits;'.
our @EXPORT = qw(CheckDigits);
our @ISA = qw(Algorithm::CheckDigits);
# These variables store the keys under which the variants of the algorithm in
# this module are registered with Algorithm::CheckDigits. They must be made
# publicly accessible for the user of this module.
our $meth1 = Algorithm::CheckDigits::plug_in('PluginLibA', 'returns 1', 'pla');
our $meth2 = Algorithm::CheckDigits::plug_in('PluginLibA', 'returns 2', 'pla');
# It's possible to use the -> notation to access the plug_in() function.
our $meth3 = Algorithm::CheckDigits->plug_in('PluginLibA', 'returns 3', 'pla');
# Since this module provides variants of the algorithm it stores the key used
# to create the instance, which is given as the first argument after the class
t/PluginLibB.pm view on Meta::CPAN
# stand-alone. As a plugin one can create instances with the CheckDigits()
# function from Algorithm::CheckDigits. If that modulue is not available,
# it is possible to create instances with PluginLibB->new().
# See pluginlibb.t and pluginlibb-without.t for example usage.
use strict;
use warnings;
# These variables store the keys under which the variants of the algorithm in
# this module will be registered with Algorithm::CheckDigits if that module is
# available. They must be made publicly accessible for the user of this module.
#
# If there are no variations of the algorithm, it is not necessary to use such
# variables in stand-alone mode. But for usage together with
# Algorithm::CheckDigits at least one method per variant is mandatory.
our ($meth1,$meth2,$meth3);
# Since this module should work regardless of the availability of
# Algorithm::CheckDigits we have to 'eval "use Algorithm::CheckDigits";'.
t/plugina.t view on Meta::CPAN
use lib qw( t );
# Since the module PluginLibA reexports the function CheckDigits() from
# Algorithm::CheckDigits, we do not need to explicitly use that module to get
# the function into our name space.
use PluginLibA;
# To get access to the algorithm provided by module PluginLibA, we have to use
# the keys stored in these publicly accessible variables.
my $cd1 = CheckDigits($PluginLibA::meth1);
my $cd2 = CheckDigits($PluginLibA::meth2);
my $cd3 = CheckDigits($PluginLibA::meth3);
is($cd1->checkdigit(234), 1, 'checked method1 of PluginLibA');
is($cd2->basenumber(234), 2, 'checked method2 of PluginLibA');
is($cd3->complete(234), 3, 'checked method3 of PluginLibA');
done_testing();
t/pluginb-without.t view on Meta::CPAN
use Test::More;
use lib qw( t );
use PluginLibB;
# To get access to the algorithm provided by module PluginLibB, we have to use
# the keys stored in these publicly accessible variables.
my $cd1 = PluginLibB->new($PluginLibB::meth1);
my $cd2 = PluginLibB->new($PluginLibB::meth2);
my $cd3 = PluginLibB->new($PluginLibB::meth3);
is($cd1->checkdigit(234), 1, 'checked method1 of PluginLibB');
is($cd2->basenumber(234), 2, 'checked method2 of PluginLibB');
is($cd3->complete(234), 3, 'checked method3 of PluginLibB');
done_testing();
t/pluginb.t view on Meta::CPAN
use lib qw( t );
# Since PluginLibB does not inherit from Algorithm::CheckDigits we have to use
# Algorithm::CheckDigits here to make use of the function CheckDigits() to get
# our algorithm checker.
#
use Algorithm::CheckDigits;
use PluginLibB;
# To get access to the algorithm provided by module PluginLibB, we have to use
# the keys stored in these publicly accessible variables.
my $cd1 = CheckDigits($PluginLibB::meth1);
my $cd2 = CheckDigits($PluginLibB::meth2);
my $cd3 = CheckDigits($PluginLibB::meth3);
is($cd1->checkdigit(234), 1, 'checked method1 of PluginLibB');
is($cd2->basenumber(234), 2, 'checked method2 of PluginLibB');
is($cd3->complete(234), 3, 'checked method3 of PluginLibB');
done_testing();
( run in 0.927 second using v1.01-cache-2.11-cpan-64827b87656 )