| 1 | package Function::Register; | ||||||||||||
| 2 | # $Id: Register.pm,v 1.2 2004/05/21 05:18:19 cwest Exp $ | ||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use vars qw[$VERSION %REGISTRIES %REGISTRANTS]; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | $VERSION = (qw$Revision: 1.2 $)[1]; | |||||||||||
| 7 | |||||||||||||
| 8 | =head1 NAME | ||||||||||||
| 9 | |||||||||||||
| 10 | Function::Register - Create Function Registries and Register Functions | ||||||||||||
| 11 | |||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||
| 13 | |||||||||||||
| 14 | package Company::Employee; | ||||||||||||
| 15 | use Function::Register; | ||||||||||||
| 16 | set_register 'Type'; | ||||||||||||
| 17 | |||||||||||||
| 18 | sub employee_type { | ||||||||||||
| 19 | my $self = shift; | ||||||||||||
| 20 | for ( @Type ) { | ||||||||||||
| 21 | my $retval = $_->($self); | ||||||||||||
| 22 | return $retval if $retval; | ||||||||||||
| 23 | } | ||||||||||||
| 24 | return; | ||||||||||||
| 25 | } | ||||||||||||
| 26 | |||||||||||||
| 27 | |||||||||||||
| 28 | # meanwhile, in some other package | ||||||||||||
| 29 | package Company::Employee::Executive; | ||||||||||||
| 30 | use Function::Register 'Company::Employee'; | ||||||||||||
| 31 | |||||||||||||
| 32 | register Type => \&is_cto; | ||||||||||||
| 33 | register Type => \&is_ceo; | ||||||||||||
| 34 | |||||||||||||
| 35 | sub is_cto { ... } | ||||||||||||
| 36 | sub is_ceo { ... } | ||||||||||||
| 37 | |||||||||||||
| 38 | # meanwhile, in your program | ||||||||||||
| 39 | use Company::Employee; | ||||||||||||
| 40 | use Company::Employee::Executive; | ||||||||||||
| 41 | |||||||||||||
| 42 | my $employee = Company::Employee->new( title => "CEO", ... ); | ||||||||||||
| 43 | print $employee->employee_type; | ||||||||||||
| 44 | |||||||||||||
| 45 | =head1 DESCRIPTION | ||||||||||||
| 46 | |||||||||||||
| 47 | This module allows you to declare registers in your namespace, and update | ||||||||||||
| 48 | registers in other modules. | ||||||||||||
| 49 | |||||||||||||
| 50 | =head2 Exports | ||||||||||||
| 51 | |||||||||||||
| 52 | There are two ways to use this modules. | ||||||||||||
| 53 | |||||||||||||
| 54 | =over 4 | ||||||||||||
| 55 | |||||||||||||
| 56 | =item As the Registry | ||||||||||||
| 57 | |||||||||||||
| 58 | use Function::Register; | ||||||||||||
| 59 | |||||||||||||
| 60 | As the registry you simply use the module without any arguments. This will | ||||||||||||
| 61 | export the C<set_register> function. It will also create a default register | ||||||||||||
| 62 | in your namespace called C<@REGISTER>. | ||||||||||||
| 63 | |||||||||||||
| 64 | =item As the Registrant | ||||||||||||
| 65 | |||||||||||||
| 66 | use Function::Register qw[Some::NameSpace]; | ||||||||||||
| 67 | |||||||||||||
| 68 | As the registrant you use the module with a single argument. This will | ||||||||||||
| 69 | export the C<register> function. It will remember what namespace you | ||||||||||||
| 70 | want to add to each time you call C<register>. | ||||||||||||
| 71 | |||||||||||||
| 72 | =back | ||||||||||||
| 73 | |||||||||||||
| 74 | =cut | ||||||||||||
| 75 | |||||||||||||
| 76 | 2 | ▶ | sub import { | ||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||
| 77 | 2 | my ($class, $registry) = @_; | |||||||||||
| 78 | 2 | ▶ | if ( $registry ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | 1 | my $registrant = caller; | |||||||||||
| 80 | 1 | $REGISTRANTS{$registrant} = $registry; | |||||||||||
| 81 | 1 | _export( $registrant, 'register' ); | |||||||||||
| 82 | } else { | ||||||||||||
| 83 | 1 | _export( scalar(caller()), 'set_register' ); | |||||||||||
| 84 | 1 | @_ = (); | |||||||||||
| 85 | 1 | goto &set_register; | |||||||||||
| 86 | } | ||||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | =head2 Functions | ||||||||||||
| 90 | |||||||||||||
| 91 | =over 4 | ||||||||||||
| 92 | |||||||||||||
| 93 | =item set_registry | ||||||||||||
| 94 | |||||||||||||
| 95 | set_registry 'Name'; | ||||||||||||
| 96 | |||||||||||||
| 97 | This function creates a new register in your namespace. A register is a | ||||||||||||
| 98 | package array of the same name. The call above creates an array, C<@Name>, | ||||||||||||
| 99 | in your namespace. | ||||||||||||
| 100 | |||||||||||||
| 101 | =cut | ||||||||||||
| 102 | |||||||||||||
| 103 | 3 | ▶ | sub set_register($) { | ||||||||||
|
Subroutine
set_register: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 104 | 3 | ▶ | my $registry = shift() || 'REGISTER'; | ||||||||||
|
Condition100%2/2
shift() || 'REGISTER'
MC/DC100%1/1
shift() || 'REGISTER'
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 105 | 3 | my $package = caller; | |||||||||||
| 106 | 3 | $REGISTRIES{$package}->{$registry} = 1; | |||||||||||
| 107 | 3 | my @reg; | |||||||||||
| 108 | 1 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 109 | 3 | *{"$package\::$registry"} = \@reg; | |||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | =item register | ||||||||||||
| 113 | |||||||||||||
| 114 | register sub { ... }; | ||||||||||||
| 115 | register Name => \&function_ref; | ||||||||||||
| 116 | |||||||||||||
| 117 | This function registeres functions in the namespace you've declared | ||||||||||||
| 118 | as your registrant. If a single argument is given the function is | ||||||||||||
| 119 | added to the default registry. If two arguments are given, the first | ||||||||||||
| 120 | is the name of of the register and the second is a function. | ||||||||||||
| 121 | |||||||||||||
| 122 | This function returns a false value if it was unable to add the | ||||||||||||
| 123 | function to the register. This may be because the register name | ||||||||||||
| 124 | does not exist, or the function argument isn't a code reference. | ||||||||||||
| 125 | |||||||||||||
| 126 | If C<register> is successful it returns true. | ||||||||||||
| 127 | |||||||||||||
| 128 | die "Couldn't add to register" | ||||||||||||
| 129 | unless register \&some_func; | ||||||||||||
| 130 | |||||||||||||
| 131 | =back | ||||||||||||
| 132 | |||||||||||||
| 133 | =cut | ||||||||||||
| 134 | |||||||||||||
| 135 | 8 | ▶ | sub register($;$) { | ||||||||||
|
Subroutine
register: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 136 | 8 | my $reg = 'REGISTER'; | |||||||||||
| 137 | 8 | my $func; | |||||||||||
| 138 | 8 | ▶ | if ( @_ == 2) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 139 | 5 | ($reg, $func) = @_; | |||||||||||
| 140 | } else { | ||||||||||||
| 141 | 3 | ($func) = @_; | |||||||||||
| 142 | } | ||||||||||||
| 143 | 8 | ▶ | return unless ref($func) eq 'CODE'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 144 | 7 | ▶ | return unless exists $REGISTRIES{$REGISTRANTS{caller()}}->{$reg}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 145 | 6 | my $registry = join '::', $REGISTRANTS{caller()}, $reg; | |||||||||||
| 146 | 1 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 147 | 6 | unshift @{"$registry"}, $func; | |||||||||||
| 148 | } | ||||||||||||
| 149 | |||||||||||||
| 150 | 2 | ▶ | sub _export { | ||||||||||
|
Subroutine
_export: called CC=1 SCAR=0.0
| |||||||||||||
| 151 | 2 | my ($package, $name) = @_; | |||||||||||
| 152 | 1 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 153 | 2 | *{"$package\::$name"} = \&{"$name"}; | |||||||||||
| 154 | } | ||||||||||||
| 155 | |||||||||||||
| 156 | 1 | 1; | |||||||||||
| 157 | |||||||||||||
| 158 | __END__ | ||||||||||||