| 1 | package Acme::VerySign; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 3 | ▶ | use overload '""' => "as_string", | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 3 | fallback => 1; | |||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | 3 | ▶ | use Carp; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 3 | ▶ | use Devel::Symdump; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | 3 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | #use warnings; | ||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | 3 | ▶ | use vars qw($AUTOLOAD $VERSION); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | 3 | $VERSION = "1.00"; | |||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | #################################################################### | ||||||||||||||||||
| 17 | # cargo culted from Symbol::Approx::Sub | ||||||||||||||||||
| 18 | #################################################################### | ||||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | # List of functions that we _never_ try to match approximately. | ||||||||||||||||||
| 21 | 3 | my @_BARRED = qw(AUTOLOAD BEGIN CHECK INIT DESTROY END); | |||||||||||||||||
| 22 | 3 | my %_BARRED = (1) x @_BARRED; | |||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | 6 | ▶ | sub _pkg2file { | ||||||||||||||||
|
Subroutine
_pkg2file: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 25 | 6 | $_ = shift; | |||||||||||||||||
| 26 | 6 | s|::|/|g; | |||||||||||||||||
| 27 | 6 | "$_.pm"; | |||||||||||||||||
| 28 | } | ||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | #################################################################### | ||||||||||||||||||
| 31 | |||||||||||||||||||
| 32 | sub import | ||||||||||||||||||
| 33 | 3 | ▶ | { | ||||||||||||||||
|
Subroutine
import: called CC=14 SCAR=37.7
| |||||||||||||||||||
| 34 | 3 | my $class = shift; | |||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | # work out who called us | ||||||||||||||||||
| 37 | 3 | my $pkg = caller(0); | |||||||||||||||||
| 38 | |||||||||||||||||||
| 39 | # turn off refs while we write to another package namespace | ||||||||||||||||||
| 40 | 3 | ▶ | no strict "refs"; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | #################################################################### | ||||||||||||||||||
| 43 | # cargo culted from Symbol::Approx::Sub | ||||||||||||||||||
| 44 | #################################################################### | ||||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | 3 | my %param; | |||||||||||||||||
| 47 | my %CONF; | ||||||||||||||||||
| 48 | 3 | ▶ | %param = @_ if @_; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | 3 | my %defaults = (xform => 'Text::Soundex', | |||||||||||||||||
| 51 | match => 'String::Equal', | ||||||||||||||||||
| 52 | choose => 'Random'); | ||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | # Work out which transformer(s) to use. The valid options are: | ||||||||||||||||||
| 55 | # 1/ $param{xform} doesn't exist. Use default transformer. | ||||||||||||||||||
| 56 | # 2/ $param{xform} is undef. Use no transformers. | ||||||||||||||||||
| 57 | # 3/ $param{xform} is a reference to a subroutine. Use the | ||||||||||||||||||
| 58 | # referenced subroutine as the transformer. | ||||||||||||||||||
| 59 | # 4/ $param{xform} is a scalar. This is the name of a transformer | ||||||||||||||||||
| 60 | # module which should be loaded. | ||||||||||||||||||
| 61 | # 5/ $param{xform} is a reference to an array. Each element of the | ||||||||||||||||||
| 62 | # array is one of the previous two options. | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | 3 | ▶ | if (exists $param{xform}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 65 | 1 | ▶ | if (defined $param{xform}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 66 | 1 | my $type = ref $param{xform}; | |||||||||||||||||
| 67 | 1 | ▶ | if ($type eq 'CODE') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 68 | 0 | $CONF{xform} = [$param{xform}]; | |||||||||||||||||
| 69 | } elsif ($type eq '') { | ||||||||||||||||||
| 70 | 1 | my $mod = "Symbol::Approx::Sub::$param{xform}"; | |||||||||||||||||
| 71 | 1 | require(_pkg2file($mod)); | |||||||||||||||||
| 72 | 1 | $CONF{xform} = [\&{"${mod}::transform"}]; | |||||||||||||||||
| 73 | } elsif ($type eq 'ARRAY') { | ||||||||||||||||||
| 74 | 0 | foreach (@{$param{xform}}) { | |||||||||||||||||
| 75 | 0 | my $type = ref $_; | |||||||||||||||||
| 76 | 0 | ▶ | if ($type eq 'CODE') { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 77 | 0 | push @{$CONF{xform}}, $_; | |||||||||||||||||
| 78 | } elsif ($type eq '') { | ||||||||||||||||||
| 79 | 0 | my $mod = "Symbol::Approx::Sub::$_"; | |||||||||||||||||
| 80 | 0 | require(_pkg2file($mod)); | |||||||||||||||||
| 81 | 0 | push @{$CONF{xform}}, \&{"${mod}::transform"}; | |||||||||||||||||
| 82 | } else { | ||||||||||||||||||
| 83 | 0 | croak 'Invalid transformer passed to Acme::VerySign'; | |||||||||||||||||
| 84 | } | ||||||||||||||||||
| 85 | } | ||||||||||||||||||
| 86 | } else { | ||||||||||||||||||
| 87 | 0 | croak 'Invalid transformer passed to Acme::VerySign'; | |||||||||||||||||
| 88 | } | ||||||||||||||||||
| 89 | } else { | ||||||||||||||||||
| 90 | 0 | $CONF{xform} = []; | |||||||||||||||||
| 91 | } | ||||||||||||||||||
| 92 | } else { | ||||||||||||||||||
| 93 | 2 | my $mod = "Symbol::Approx::Sub::$defaults{xform}"; | |||||||||||||||||
| 94 | 2 | require(_pkg2file($mod)); | |||||||||||||||||
| 95 | 2 | $CONF{xform} = [\&{"${mod}::transform"}]; | |||||||||||||||||
| 96 | } | ||||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | # Work out which matcher to use. The valid options are: | ||||||||||||||||||
| 99 | # 1/ $param{match} doesn't exist. Use default matcher. | ||||||||||||||||||
| 100 | # 2/ $param{match} is undef. Use no matcher. | ||||||||||||||||||
| 101 | # 3/ $param{match} is a reference to a subroutine. Use the | ||||||||||||||||||
| 102 | # referenced subroutine as the matcher. | ||||||||||||||||||
| 103 | # 4/ $param{match} is a scalar. This is the name of a matcher | ||||||||||||||||||
| 104 | # module which should be loaded. | ||||||||||||||||||
| 105 | |||||||||||||||||||
| 106 | 3 | ▶ | if (exists $param{match}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 107 | 0 | ▶ | if (defined $param{match}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 108 | 0 | my $type = ref $param{match}; | |||||||||||||||||
| 109 | 0 | ▶ | if ($type eq 'CODE') { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 110 | 0 | $CONF{match} = $param{match}; | |||||||||||||||||
| 111 | } elsif ($type eq '') { | ||||||||||||||||||
| 112 | 0 | my $mod = "Symbol::Approx::Sub::$param{match}"; | |||||||||||||||||
| 113 | 0 | require(_pkg2file($mod)); | |||||||||||||||||
| 114 | 0 | $CONF{match} = \&{"${mod}::match"}; | |||||||||||||||||
| 115 | } else { | ||||||||||||||||||
| 116 | 0 | croak 'Invalid matcher passed to Symbol::Approx::Sub'; | |||||||||||||||||
| 117 | } | ||||||||||||||||||
| 118 | } else { | ||||||||||||||||||
| 119 | 0 | $CONF{match} = undef; | |||||||||||||||||
| 120 | } | ||||||||||||||||||
| 121 | } else { | ||||||||||||||||||
| 122 | 3 | my $mod = "Symbol::Approx::Sub::$defaults{match}"; | |||||||||||||||||
| 123 | 3 | require(_pkg2file($mod)); | |||||||||||||||||
| 124 | 3 | $CONF{match} = \&{"${mod}::match"}; | |||||||||||||||||
| 125 | } | ||||||||||||||||||
| 126 | |||||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | #################################################################### | ||||||||||||||||||
| 129 | |||||||||||||||||||
| 130 | # install the AUTOLOAD method | ||||||||||||||||||
| 131 | 3 | ▶ | *{"${pkg}::AUTOLOAD"} = sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 132 | 5 | Acme::VerySign->new($AUTOLOAD =~ /^(.*)::(.*)$/, %CONF); | |||||||||||||||||
| 133 | } | ||||||||||||||||||
| 134 | 3 | } | |||||||||||||||||
| 135 | |||||||||||||||||||
| 136 | { | ||||||||||||||||||
| 137 | 3 | my $pkg; | |||||||||||||||||
| 138 | 3 | my $subname; | |||||||||||||||||
| 139 | |||||||||||||||||||
| 140 | sub new | ||||||||||||||||||
| 141 | 5 | ▶ | { | ||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||
| 142 | 5 | my $class = shift; | |||||||||||||||||
| 143 | 5 | $pkg = shift; | |||||||||||||||||
| 144 | 5 | my $sub = $subname = shift; | |||||||||||||||||
| 145 | 5 | my %CONF = @_; | |||||||||||||||||
| 146 | |||||||||||||||||||
| 147 | #################################################################### | ||||||||||||||||||
| 148 | # code cargo culted from Symbol::Approx::Sub | ||||||||||||||||||
| 149 | #################################################################### | ||||||||||||||||||
| 150 | |||||||||||||||||||
| 151 | # Get a list of all of the subroutines in the current package | ||||||||||||||||||
| 152 | # using the get_subs function from GlobWalker.pm | ||||||||||||||||||
| 153 | # Note that we deliberately omit function names that exist | ||||||||||||||||||
| 154 | # in the %_BARRED hash | ||||||||||||||||||
| 155 | 5 | my (@subs, @orig); | |||||||||||||||||
| 156 | 5 | my $sym = Devel::Symdump->new($pkg); | |||||||||||||||||
| 157 | 140 | @orig = @subs = grep { ! $_BARRED{$_} } | |||||||||||||||||
| 158 | 140 | map { s/${pkg}:://; $_ } | |||||||||||||||||
| 159 | 5 | grep { defined &{$_} } $sym->functions($pkg); | |||||||||||||||||
| 160 | |||||||||||||||||||
| 161 | # Transform all of the subroutine names | ||||||||||||||||||
| 162 | 5 | foreach (@{$CONF{xform}}) { | |||||||||||||||||
| 163 | 5 | ▶ | croak "Invalid transformer passed to Acme::VerySign\n" | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 164 | unless defined &$_; | ||||||||||||||||||
| 165 | 5 | ($sub, @subs) = $_->($sub, @subs); | |||||||||||||||||
| 166 | } | ||||||||||||||||||
| 167 | |||||||||||||||||||
| 168 | # Call the subroutine that will look for matches | ||||||||||||||||||
| 169 | # The matcher returns a list of the _indexes_ that match | ||||||||||||||||||
| 170 | 5 | my @match_ind; | |||||||||||||||||
| 171 | 5 | ▶ | if ($CONF{match}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 172 | croak "Invalid matcher passed to Acme::VerySign\n" | ||||||||||||||||||
| 173 | 5 | ▶ | unless defined &{$CONF{match}}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 174 | 5 | @match_ind = $CONF{match}->($sub, @subs); | |||||||||||||||||
| 175 | } else { | ||||||||||||||||||
| 176 | 0 | @match_ind = (0..$#subs); | |||||||||||||||||
| 177 | } | ||||||||||||||||||
| 178 | |||||||||||||||||||
| 179 | 5 | @orig = @orig[@match_ind]; | |||||||||||||||||
| 180 | |||||||||||||||||||
| 181 | |||||||||||||||||||
| 182 | #################################################################### | ||||||||||||||||||
| 183 | |||||||||||||||||||
| 184 | # unique that array | ||||||||||||||||||
| 185 | 5 | my %orig = map { $_ => 1 } @orig; | |||||||||||||||||
| 186 | 5 | my $this = bless [keys %orig], $class; | |||||||||||||||||
| 187 | 5 | return $this; | |||||||||||||||||
| 188 | } | ||||||||||||||||||
| 189 | |||||||||||||||||||
| 190 | 2 | ▶ | sub as_string { "64.94.110.11" } | ||||||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 191 | |||||||||||||||||||
| 192 | sub buy | ||||||||||||||||||
| 193 | 1 | ▶ | { | ||||||||||||||||
|
Subroutine
buy: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||
| 194 | 1 | my $this = shift; | |||||||||||||||||
| 195 | 1 | ▶ | die "No matching subroutines!" unless defined $this->[0]; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 196 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 197 | 1 | *{"${pkg}::${subname}"} = *{"${pkg}::".$this->[0]}{CODE}; | |||||||||||||||||
| 198 | } | ||||||||||||||||||
| 199 | |||||||||||||||||||
| 200 | } | ||||||||||||||||||
| 201 | |||||||||||||||||||
| 202 | 3 | 1; | |||||||||||||||||
| 203 | |||||||||||||||||||
| 204 | __END__ | ||||||||||||||||||