| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | package DBIx::Class::ResultSet::SetControl; | ||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use DBIx::Class::Util::ResultSet::Iterator; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | sub each { | ||||||||||||||||||||||||||||||||||||||||||
| 8 | 8 | ▶ | my($self, $func_proto, $fail) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
each: called CC=8 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | ## validate and normalize the function prototype | ||||||||||||||||||||||||||||||||||||||||||
| 11 | 8 | ▶ | unless( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$func_proto and ref $func_proto eq "CODE" || ref $func_proto eq "ARRAY"
ref $func_proto eq "CODE" or ref $func_proto eq "ARRAY"
MC/DC0%0/3
$func_proto and ref $func_proto eq "CODE" || ref $func_proto eq "ARRAY"
$func_proto
ref $func_proto eq "CODE"
ref $func_proto eq "ARRAY"
Truth table25%1/4
A and B or C
A= $func_protoB= ref $func_proto eq "CODE"C= ref $func_proto eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | $func_proto and ( | ||||||||||||||||||||||||||||||||||||||||||
| 13 | ref($func_proto) eq 'CODE' or | ||||||||||||||||||||||||||||||||||||||||||
| 14 | ref($func_proto) eq 'ARRAY' ) | ||||||||||||||||||||||||||||||||||||||||||
| 15 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | $self->throw_exception('Argument must be a CODEREF or ARRAYREF') | |||||||||||||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | ## create a local iterator for the each function | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 8 | my @funcs; | |||||||||||||||||||||||||||||||||||||||||
| 21 | my $func_itr = sub { | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 25 | ▶ | @funcs = ref($func_proto) eq 'ARRAY' ? @$func_proto : $func_proto unless @funcs; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 25 | return shift @funcs; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 8 | }; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | ## Iterate over the resultset | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 8 | my $itr = DBIx::Class::Util::ResultSet::Iterator->new(resultset=>$self); | |||||||||||||||||||||||||||||||||||||||||
| 28 | 8 | ▶ | while(my $row = $itr->next) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 25 | { local $_ = $row; $func_itr->()->($itr, $row) }; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 25 | ▶ | if($itr->has_escaped) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | return $itr->resultset; | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | ## Handle fails | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 7 | ▶ | if($fail && $itr->has_not_been_used) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$fail and $itr->has_not_been_used
MC/DC100%2/2
$fail and $itr->has_not_been_used
$fail
$itr->has_not_been_used
Truth table100%3/3
A and B
A= $failB= $itr->has_not_been_used
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | $fail->($self); | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | ## Allow chaining methods | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 7 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | sub once { | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 2 | ▶ | my($self, $func, $fail) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
once: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 2 | ▶ | if(my $row = $self->next) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | { local $_ = $row; $func->($row) }; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } elsif($fail) { | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | $fail->(); | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | sub tap { | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | ▶ | my($self, $func, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 4 | my $new_rs = $self->search_rs; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 4 | { local $_ = $new_rs; $func->($func, $new_rs, @args) }; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 4 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | sub times { | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | ▶ | my($self, $times, $func, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
times: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | $self->tap($func, @args) for 1.. $times; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | DBIx::Class::ResultSet::SetControl - Easier Looping over resultsets | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | Given a L<DBIx::Class::ResultSet> that consumes this component, such as the | ||||||||||||||||||||||||||||||||||||||||||
| 76 | following: | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | package MySchema::ResultSet::Bar; | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | use strict; | ||||||||||||||||||||||||||||||||||||||||||
| 81 | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
| 82 | use parent 'DBIx::Class::ResultSet'; | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | __PACKAGE__->load_components('ResultSet::SetControl'); | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | ## Additional custom resultset methods, if any | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | Then later when you have a resultset of that class: | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | my $rs = $schema->resultset('Bar'); | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | You can call methods directly on your object which are related to control flow | ||||||||||||||||||||||||||||||||||||||||||
| 95 | and looping over the items in you resultset. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | $rs->tap(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 98 | print shift->find({id=>1} ? 'found one' : 'nope'; | ||||||||||||||||||||||||||||||||||||||||||
| 99 | })->each(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 100 | my ($each, $row) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 101 | print $each->is_odd ? $row->name . ' is odd' : 'nope, not odd'; | ||||||||||||||||||||||||||||||||||||||||||
| 102 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | B<NOTE> If you intend to use this component in many of your resultsets, its best | ||||||||||||||||||||||||||||||||||||||||||
| 105 | practice to write a 'base' resultset that loads this (and any other) components | ||||||||||||||||||||||||||||||||||||||||||
| 106 | from which all your custom resultset classes inherit. | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | There are times where Perl's procedural syntax for control flow and looping | ||||||||||||||||||||||||||||||||||||||||||
| 111 | leads to excessively verbose code. For those times we present this helper | ||||||||||||||||||||||||||||||||||||||||||
| 112 | which is designed to encapsulate some very common control flow and loop patterns | ||||||||||||||||||||||||||||||||||||||||||
| 113 | for L<DBIx::Class> users. | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | The methods are OO in nature and designed to be compact and concise. | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | Additionally, we have tried to write these methods to allow for a 'chaining' | ||||||||||||||||||||||||||||||||||||||||||
| 118 | approach that you can't replicate with traditional Perl control and looping | ||||||||||||||||||||||||||||||||||||||||||
| 119 | structures. Each control flow method returns the original resultset so you | ||||||||||||||||||||||||||||||||||||||||||
| 120 | can proceed as though it is unaltered (unless of course you alter it somehow | ||||||||||||||||||||||||||||||||||||||||||
| 121 | like with an insert or update). | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | The goal it to help avoid excessive conditional logic and to allow one to write | ||||||||||||||||||||||||||||||||||||||||||
| 124 | more compact and neat code. For example, you could replace: | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | my $has_rows; | ||||||||||||||||||||||||||||||||||||||||||
| 127 | while(my $row = $rs->next) { | ||||||||||||||||||||||||||||||||||||||||||
| 128 | $has_rows = 1; | ||||||||||||||||||||||||||||||||||||||||||
| 129 | ## Do something | ||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | unless($has_rows) { | ||||||||||||||||||||||||||||||||||||||||||
| 132 | warn 'no rows!'; | ||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | With something like | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | $rs->each(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 138 | my ($each, $row) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 139 | ## Do Something | ||||||||||||||||||||||||||||||||||||||||||
| 140 | }, sub { | ||||||||||||||||||||||||||||||||||||||||||
| 141 | warn 'no rows!'; | ||||||||||||||||||||||||||||||||||||||||||
| 142 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | The second version has less overall lines and characters, and it also carefully | ||||||||||||||||||||||||||||||||||||||||||
| 145 | encapsulates a very common pattern, which is to loop over all the rows in a | ||||||||||||||||||||||||||||||||||||||||||
| 146 | resultset and do something should no rows exist. Also, the L</each> method | ||||||||||||||||||||||||||||||||||||||||||
| 147 | returns the original C<$rs> so you could chain commands: | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | $rs->each(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 150 | my ($each, $row) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 151 | ## Do Something | ||||||||||||||||||||||||||||||||||||||||||
| 152 | }, sub { | ||||||||||||||||||||||||||||||||||||||||||
| 153 | warn 'no rows!'; | ||||||||||||||||||||||||||||||||||||||||||
| 154 | })->tap(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 155 | my $rs = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 156 | ## Do something else | ||||||||||||||||||||||||||||||||||||||||||
| 157 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | There may be cases in your logical flow where this type of programming is more | ||||||||||||||||||||||||||||||||||||||||||
| 160 | clear and simple; in other cases traditional Perl control and looping might be | ||||||||||||||||||||||||||||||||||||||||||
| 161 | better. These methods give you an option. On the other hand you might think | ||||||||||||||||||||||||||||||||||||||||||
| 162 | this is all pointless line noise. As you wish :) | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | This component defines the following methods. | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | =head2 each | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | Arguments: $rs->each($coderef|\@$coderef, ?$if_empty_coderef) | ||||||||||||||||||||||||||||||||||||||||||
| 171 | Returns: Original Resultset OR partly iterated Resultset | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | Where C<$coderef> is an anonymous subroutine or closure that will get the | ||||||||||||||||||||||||||||||||||||||||||
| 174 | instantiated L<DBIx::Class::Helpers::Util::ResultSet::Iterator> object and the | ||||||||||||||||||||||||||||||||||||||||||
| 175 | current C<$row> from the set returned. For example C<$row> in the ResultSet | ||||||||||||||||||||||||||||||||||||||||||
| 176 | the $coderef will be executed once. | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | C<$if_empty_coderef> is an anonymous subroutine or closure that gets | ||||||||||||||||||||||||||||||||||||||||||
| 179 | executed ONLY if there were no rows in the set. It gets the C<$resultset> | ||||||||||||||||||||||||||||||||||||||||||
| 180 | as an argument (this might change later if we discover a better thing to do | ||||||||||||||||||||||||||||||||||||||||||
| 181 | here). | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | In the case where the first argument is an arrayref of coderefs, we automatically | ||||||||||||||||||||||||||||||||||||||||||
| 184 | iterate over each coderef for each result in the set in turn and reset the | ||||||||||||||||||||||||||||||||||||||||||
| 185 | coderef iterator as needed to make sure we hit every item in the set. Please | ||||||||||||||||||||||||||||||||||||||||||
| 186 | be aware that in the case where the arrayref of coderefs is longer than the | ||||||||||||||||||||||||||||||||||||||||||
| 187 | available results in the set, this means that not all coderefs will be invoked | ||||||||||||||||||||||||||||||||||||||||||
| 188 | and this happens without an exception being thrown. | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | Example: For the given L<DBIx::Class::ResultSet>, iterator over each result. | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | $rs->each(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 193 | my ($each, $row) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 194 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 195 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | This is functionally similar to something like: | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | my $itr = DBIx::Class::Util::ResultSet::Iterator->new(resultset=>$rs); | ||||||||||||||||||||||||||||||||||||||||||
| 200 | while(my $row = $itr->next) { | ||||||||||||||||||||||||||||||||||||||||||
| 201 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | However the method will return the original $resultset used to initialize it | ||||||||||||||||||||||||||||||||||||||||||
| 205 | so that you can continue chaining or building off it. Of course you will need | ||||||||||||||||||||||||||||||||||||||||||
| 206 | to issue a c<ResultSet->reset> for this to be useful. | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | Here's a more detailed example. | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | $rs->each(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 211 | my ($each, $row) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | $each->first(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 214 | print "Hey, this is the first row!"; | ||||||||||||||||||||||||||||||||||||||||||
| 215 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | if($each->is_odd) { | ||||||||||||||||||||||||||||||||||||||||||
| 218 | print $row->columnname; | ||||||||||||||||||||||||||||||||||||||||||
| 219 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 220 | return $each->escape; | ||||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | }, sub { | ||||||||||||||||||||||||||||||||||||||||||
| 223 | my ($rs) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 224 | warn "The resultset was empty, nothing done..."; | ||||||||||||||||||||||||||||||||||||||||||
| 225 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | Finally one example using an arrayref as the first argument: | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | $rs->each( | ||||||||||||||||||||||||||||||||||||||||||
| 230 | [ | ||||||||||||||||||||||||||||||||||||||||||
| 231 | sub { ... }, | ||||||||||||||||||||||||||||||||||||||||||
| 232 | sub { ... }, | ||||||||||||||||||||||||||||||||||||||||||
| 233 | sub { ... }, | ||||||||||||||||||||||||||||||||||||||||||
| 234 | ], sub { | ||||||||||||||||||||||||||||||||||||||||||
| 235 | my ($rs) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 236 | warn "The resultset was empty, nothing done..."; | ||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | You may find this helper leads you to writing more concise and compact code. | ||||||||||||||||||||||||||||||||||||||||||
| 241 | Additionally having an iterator object available can be helpful, particularly | ||||||||||||||||||||||||||||||||||||||||||
| 242 | when you are in a template and need to display things differently based on if | ||||||||||||||||||||||||||||||||||||||||||
| 243 | the row is even/odd, first/last, etc. | ||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | You should see the documentation for L<DBIx::Class::Util::ResultSet::Iterator> | ||||||||||||||||||||||||||||||||||||||||||
| 246 | for the methods this object exposes for use. | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | B<NOTE> For conciseness in simple cases, we overload C<$_> to equal the value of | ||||||||||||||||||||||||||||||||||||||||||
| 249 | C<$row> as described above. | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | =head2 once | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | Arguments: $rs->once($coderef, ?$if_empty_coderef) | ||||||||||||||||||||||||||||||||||||||||||
| 254 | Returns: Partly iterated Resultset | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | Works just like L</each> expect instead of iterating over the entire resultset | ||||||||||||||||||||||||||||||||||||||||||
| 257 | we just take the first C<$row>. | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | Example | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | $rs->once( sub { | ||||||||||||||||||||||||||||||||||||||||||
| 262 | my ($row) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 263 | }, sub { | ||||||||||||||||||||||||||||||||||||||||||
| 264 | warn 'no rows left!'; | ||||||||||||||||||||||||||||||||||||||||||
| 265 | })->each( ... ); | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | Useful to isolate the logic for the first row in a resultset. | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | B<NOTE> For conciseness in simple cases, we overload C<$_> to equal the value of | ||||||||||||||||||||||||||||||||||||||||||
| 270 | C<$row> as described above. | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | =head2 tap | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | Arguments: $coderef, ?@args | ||||||||||||||||||||||||||||||||||||||||||
| 275 | Returns: Original Resultset | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | Do a coderef with the resultset passed as an argument. | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | $rs->tap(sub { | ||||||||||||||||||||||||||||||||||||||||||
| 280 | my ($func, $rs, $arg) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 281 | $rs->find({id=>$arg}); | ||||||||||||||||||||||||||||||||||||||||||
| 282 | }, 100); | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | If you pass more than one argument, all the extra arguments will be send to the | ||||||||||||||||||||||||||||||||||||||||||
| 285 | anonymous coderef. | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | B<NOTE> The resultset passed is cloned from the original and it returns the original | ||||||||||||||||||||||||||||||||||||||||||
| 288 | resultset so you can chain. | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | B<NOTE> the first argument passed to the coderef is the orginal coderef so that you can | ||||||||||||||||||||||||||||||||||||||||||
| 291 | call recursively. | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | B<NOTE> For conciseness in simple cases, we overload C<$_> to equal the value of | ||||||||||||||||||||||||||||||||||||||||||
| 294 | C<$rs> as described above. | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | =head2 times | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | Arguments: $integer, $coderef, ?@args | ||||||||||||||||||||||||||||||||||||||||||
| 299 | Returns: Original Resultset | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | Basically this calls L</tap> a number of times equal to the first argument. | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | $rs->times(3, sub { | ||||||||||||||||||||||||||||||||||||||||||
| 304 | my $rs = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 305 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 306 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | B<NOTE> For conciseness in simple cases, we overload C<$_> to equal the value of | ||||||||||||||||||||||||||||||||||||||||||
| 309 | C<$rs> as described above. | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | L<DBIx::Class> | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | Copyright 2017, John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 324 | the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | =cut | ||||||||||||||||||||||||||||||||||||||||||