| 1 | # Copyright 2010, 2011, 2014, 2017, 2018, 2019 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of File-Locate-Iterator. | ||
| 4 | # | ||
| 5 | # File-Locate-Iterator is free software; you can redistribute it and/or | ||
| 6 | # modify it under the terms of the GNU General Public License as published | ||
| 7 | # by the Free Software Foundation; either version 3, or (at your option) any | ||
| 8 | # later version. | ||
| 9 | # | ||
| 10 | # File-Locate-Iterator is distributed in the hope that it will be useful, | ||
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||
| 13 | # Public License for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with File-Locate-Iterator. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | package MooseX::Iterator::Locate; | ||
| 19 | use 5.006; | ||
| 20 | use Carp; | ||
| 21 | use Moose; | ||
| 22 | use Moose::Util::TypeConstraints; | ||
| 23 | use MooseX::Iterator::Meta::Iterable; | ||
| 24 | |||
| 25 | extends | ||
| 26 | 'File::Locate::Iterator', | ||
| 27 | 'Moose::Object'; # does() and stuff | ||
| 28 | with 'MooseX::Iterator::Role'; | ||
| 29 | |||
| 30 | our $VERSION = 28; | ||
| 31 | |||
| 32 | # uncomment this to run the ### lines | ||
| 33 | #use Devel::Comments; | ||
| 34 | |||
| 35 | # meta new_object() per Moose::Cookbook::Basics::Recipe11 | ||
| 36 | sub new { | ||
| 37 | my $class = shift; | ||
| 38 | my $self = $class->SUPER::new (@_); | ||
| 39 | return $class->meta->new_object (__INSTANCE__ => $self, @_); | ||
| 40 | } | ||
| 41 | |||
| 42 | # Crib: All attributes "lazy" so they're not plugged in at the new() time. | ||
| 43 | # In particular storing the "undef" defaults would interfere with the | ||
| 44 | # globs/regexps setups in File::Locate::Iterator new(), and make ->next() | ||
| 45 | # match everything. | ||
| 46 | # | ||
| 47 | |||
| 48 | has 'database_file' | ||
| 49 | => (is => 'bare', | ||
| 50 | isa => 'Maybe[Str]', | ||
| 51 | # default as subr returning value, func default_database_file() gets | ||
| 52 | # current $ENV{'LOCATE_PATH'} | ||
| 53 | default => File::Locate::Iterator->can('default_database_file'), | ||
| 54 | lazy => 1, | ||
| 55 | documentation => 'Database file name', | ||
| 56 | ); | ||
| 57 | has 'database_fh' | ||
| 58 | => (is => 'bare', | ||
| 59 | isa => 'Maybe[FileHandle]', | ||
| 60 | default => undef, | ||
| 61 | lazy => 1, | ||
| 62 | documentation => 'Database file handle', | ||
| 63 | ); | ||
| 64 | has 'database_str' | ||
| 65 | => (is => 'bare', | ||
| 66 | isa => 'Maybe[Str]', | ||
| 67 | default => undef, | ||
| 68 | lazy => 1, | ||
| 69 | documentation => 'Database contents in a string', | ||
| 70 | ); | ||
| 71 | has 'database_str_ref' | ||
| 72 | => (is => 'bare', | ||
| 73 | isa => 'Maybe[ScalarRef]', | ||
| 74 | default => undef, | ||
| 75 | lazy => 1, | ||
| 76 | documentation => 'Database contents in a ref to a string', | ||
| 77 | ); | ||
| 78 | |||
| 79 | has 'suffix' | ||
| 80 | => (is => 'bare', | ||
| 81 | isa => 'Maybe[Str]', | ||
| 82 | default => undef, | ||
| 83 | lazy => 1, | ||
| 84 | documentation => 'A suffix to match, like ".c"', | ||
| 85 | ); | ||
| 86 | has 'suffixes' | ||
| 87 | => (is => 'bare', | ||
| 88 | isa => 'Maybe[ArrayRef[Str]]', | ||
| 89 | default => undef, | ||
| 90 | lazy => 1, | ||
| 91 | documentation => 'An array of suffixes, any of which to match', | ||
| 92 | ); | ||
| 93 | |||
| 94 | has 'glob' | ||
| 95 | => (is => 'bare', | ||
| 96 | isa => 'Maybe[Str]', | ||
| 97 | default => undef, | ||
| 98 | lazy => 1, | ||
| 99 | documentation => 'A glob pattern to match, like "*.pl"', | ||
| 100 | ); | ||
| 101 | has 'globs' | ||
| 102 | => (is => 'bare', | ||
| 103 | isa => 'Maybe[ArrayRef[Str]]', | ||
| 104 | default => undef, | ||
| 105 | lazy => 1, | ||
| 106 | documentation => 'An array of glob patterns, any of which to match', | ||
| 107 | ); | ||
| 108 | |||
| 109 | has 'regexp' | ||
| 110 | => (is => 'bare', | ||
| 111 | isa => 'Maybe[Str|RegexpRef]', | ||
| 112 | default => undef, | ||
| 113 | lazy => 1, | ||
| 114 | documentation => 'A regexp to match, like qr/ban(an)*a/', | ||
| 115 | ); | ||
| 116 | has 'regexps' | ||
| 117 | => (is => 'bare', | ||
| 118 | isa => 'Maybe[ArrayRef[Str|RegexpRef]]', | ||
| 119 | default => undef, | ||
| 120 | lazy => 1, | ||
| 121 | documentation => 'An array of regexps, any of which to match', | ||
| 122 | ); | ||
| 123 | |||
| 124 | enum 'MooseX::Iterator::Locate::UseMmap' | ||
| 125 | => [ 'default', 'if_sensible', 'if_possible', '0', '1' ]; | ||
| 126 | |||
| 127 | # enum is string choices, so Maybe[] to allow undef too | ||
| 128 | has 'use_mmap' | ||
| 129 | => (is => 'bare', | ||
| 130 | isa => 'Maybe[MooseX::Iterator::Locate::UseMmap]', | ||
| 131 | default => 'default', | ||
| 132 | lazy => 1, | ||
| 133 | documentation => 'Whether to use mmap() for the database (with File::Map)', | ||
| 134 | ); | ||
| 135 | |||
| 136 | # No peek() in the underlying File::Locate::Iterator as yet, so | ||
| 137 | # peek()/next() cooperate to sit on a lookahead in $self->{'_peek'}. | ||
| 138 | # What is next() supposed to do at end of collection? | ||
| 139 | |||
| 140 | sub next { | ||
| 141 | my ($self) = @_; | ||
| 142 | ### MooseX next() ... | ||
| 143 | if (exists $self->{'_peek'}) { | ||
| 144 | ### return _peek: $self->{'_peek'} | ||
| 145 | return delete $self->{'_peek'}; | ||
| 146 | } else { | ||
| 147 | ### super NEXT | ||
| 148 | return $self->SUPER::next; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | sub has_next { | ||
| 153 | my ($self) = @_; | ||
| 154 | ### MooseX has_next() | ||
| 155 | return defined($self->peek); | ||
| 156 | } | ||
| 157 | |||
| 158 | sub peek { | ||
| 159 | my ($self) = @_; | ||
| 160 | ### MooseX peek() ... | ||
| 161 | if (exists $self->{'_peek'}) { | ||
| 162 | return $self->{'_peek'}; | ||
| 163 | } else { | ||
| 164 | ### fill from super | ||
| 165 | return ($self->{'_peek'} = $self->SUPER::next); | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | sub rewind { | ||
| 170 | my ($self) = @_; | ||
| 171 | ### MooseX rewind() ... | ||
| 172 | delete $self->{'_peek'}; | ||
| 173 | $self->SUPER::rewind; | ||
| 174 | } | ||
| 175 | BEGIN { | ||
| 176 | *reset = *rewind; | ||
| 177 | } | ||
| 178 | |||
| 179 | 1; | ||
| 180 | __END__ |