| 1 | package Memoize::HashKey::Ignore; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Syntax::Keyword::Try; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Memoize; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Memoize::HashKey::Ignore - allow certain keys not to be memoized. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 5 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | use Memoize; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | tie my %scalar_cache = 'Memoize::HashKey::Ignore', IGNORE => sub { my $key = shift, return ($key eq 'BROKENKEY') ? 1 : 0; }; | ||||||||||||||||||||||||||||
| 23 | tie my %list_cache = 'Memoize::HashKey::Ignore', IGNORE => sub { my $key = shift, return ($key eq 'BROKENKEY') ? 1 : 0; }; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | memoize('function', SCALAR_CACHE => [ HASH => \%scalar_cache ], LIST_CACHE => [ HASH => \%list_cache ]); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 EXPORT | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | Sometimes you don't want to store certain keys. You know what the values looks likes, but you can't easily write memoize function which culls them itself. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | Memoize::HashKey::Ignore allows you to supply a code reference which describes, which keys should not be stored in Memoization Cache. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | This module will allow you to memoize the entire function with splitting it into cached and uncached pieces. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 6 | ▶ | sub TIEHASH { | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=6 SCAR=18.3
| |||||||||||||||||||||||||||||
| 38 | 6 | my ($package, %args) = @_; | |||||||||||||||||||||||||||
| 39 | 6 | ▶ | my $cache = $args{HASH} || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{'HASH'} || {}
MC/DC100%1/1
$args{'HASH'} || {}
$args{'HASH'}
Truth table100%2/2
A
A= $args{'HASH'}
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 6 | ▶ | if ($args{IGNORE} and not ref $args{IGNORE} eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args{"IGNORE"} and not ref $args{"IGNORE"} eq "CODE"
MC/DC100%2/2
$args{"IGNORE"} and not ref $args{"IGNORE"} eq "CODE"
$args{"IGNORE"}
not ref $args{"IGNORE"} eq "CODE"
Truth table100%3/3
A and not(B)
A= $args{"IGNORE"}B= not ref $args{"IGNORE"} eq "CODE"
| |||||||||||||||||||||||||||||
| 42 | 1 | die 'Memoize::HashKey::Ignore: IGNORE argument must be a code ref.'; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 5 | ▶ | if ($args{TIE}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 1 | my ($module, @opts) = @{$args{TIE}}; | |||||||||||||||||||||||||||
| 46 | 1 | my $modulefile = $module . '.pm'; | |||||||||||||||||||||||||||
| 47 | 1 | $modulefile =~ s{::}{/}g; | |||||||||||||||||||||||||||
| 48 | 1 | try { require $modulefile } | |||||||||||||||||||||||||||
| 49 | 1 | catch ($e) { | |||||||||||||||||||||||||||
| 50 | 1 | die 'Memoize::HashKey::Ignore: Could not load hash tie module "' . $module . '": ' . $e; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 0 | my $rc = ( | |||||||||||||||||||||||||||
| 53 | tie %$cache => $module, | ||||||||||||||||||||||||||||
| 54 | @opts | ||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if (not $rc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | die 'Memoize::HashKey::Ignore Could not tie hash to "' . $module . '": ' . $@; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 4 | $args{CACHE} = $cache; | |||||||||||||||||||||||||||
| 62 | 4 | return bless \%args => $package; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 63 | ▶ | sub EXISTS { | ||||||||||||||||||||||||||
|
Subroutine
EXISTS: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 66 | 63 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 67 | 63 | ▶ | return (exists $self->{CACHE}->{$key}) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 36 | ▶ | sub FETCH { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | 36 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 72 | 36 | return $self->{CACHE}->{$key}; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 2 | ▶ | sub CLEAR { | ||||||||||||||||||||||||||
|
Subroutine
CLEAR: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 76 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 77 | 2 | $self->{CACHE} = {}; | |||||||||||||||||||||||||||
| 78 | 2 | return $self->{CACHE}; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 27 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 82 | 27 | my ($self, $key, $value) = @_; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 27 | ▶ | if (not defined $self->{IGNORE} or not &{$self->{IGNORE}}($key)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->{"IGNORE"} and &{$self->{"IGNORE"};}($key)
MC/DC50%1/2
defined $self->{"IGNORE"} and &{$self->{"IGNORE"};}($key)
defined $self->{"IGNORE"}
&{$self->{"IGNORE"};}($key)
Truth table66%2/3
A and B
A= defined $self->{"IGNORE"}B= &{$self->{"IGNORE"};}($key)
| |||||||||||||||||||||||||||||
| 85 | 21 | $self->{CACHE}->{$key} = $value; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 27 | return; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | binary.com, C<< <rakesh at binary.com> >> | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head1 BUGS | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Please report any bugs or feature requests to C<bug-memoize-hashkey-ignore at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 98 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Memoize-HashKey-Ignore>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 99 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | perldoc Memoize::HashKey::Ignore | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | You can also look for information at: | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =over 4 | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Memoize-HashKey-Ignore> | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | L<http://annocpan.org/dist/Memoize-HashKey-Ignore> | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | L<http://cpanratings.perl.org/d/Memoize-HashKey-Ignore> | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | L<http://search.cpan.org/dist/Memoize-HashKey-Ignore/> | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =back | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 5 | 1; # End of Memoize::HashKey::Ignore | |||||||||||||||||||||||||||