blib/lib/List/AutoNumbered.pm

filter: Statementstmt 100% 67 / 67 Branchbran 100% 18 / 18 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 22 / 22 Podpod 100% 8 / 8 totaltotal 100% 120 / 120 CC 11 SCAR 6.3
CC
11
Cov
100%
_update_lnum
4.0
new
3.0
_dor
2.0
CRAP 1.9
↑ Summary
1 package List::AutoNumbered;
2  
3 6 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 use overload
7 6 ▶ '@{}' => \&arr;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 our $VERSION = '0.000010';
10  
11 # Exports
12 6 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 our (@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
14 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 @EXPORT = qw(LSKIP);
16 6 @EXPORT_OK = qw(*TRACE); # can be localized
17 6 %EXPORT_TAGS = (
18 default => [@EXPORT],
19 all => [@EXPORT, @EXPORT_OK]
20 );
21 }
22  
23 # Imports
24 6 ▶use Getargs::Mixed;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # Documentation ======================================================== {{{1
27  
28 =head1 NAME
29  
30 List::AutoNumbered - Add sequential numbers to lists while creating them
31  
32 =head1 SYNOPSIS
33  
34 This module adds sequential numbers to lists of lists so you don't have to
35 type all the numbers. Its original use case was for adding line numbers
36 to lists of testcases. For example:
37  
38 use List::AutoNumbered; # line 1
39 my $list = List::AutoNumbered->new(__LINE__); # line 2
40 $list->load("a")-> # line 3
41 ("b") # line 4
42 ("c") # line 5
43 ("d"); # line 6
44  
45 # Now $list->arr is [ [3,"a"], [4,"b"], [5,"c"], [6,"d"] ]
46 # and @$list is ( [3,"a"] ... ).
47  
48 In general, you can pass any number to the constructor. For example:
49  
50 use List::AutoNumbered;
51 use Test::More tests => 1;
52  
53 my $list = List::AutoNumbered->new; # First entry will be number 1
54 $list->load("a")-> # Yes, trailing arrow
55 ("b") # Magic! Don"t need any more arrows!
56 ("c")
57 ("d");
58  
59 is_deeply($list->arr, [
60 [1, "a"], [2, "b"], [3, "c"], [4, "d"]
61 ]); # Yes, it is!
62  
63 =cut
64  
65 6 our $TRACE = 0; # Documented below
66  
67 =head1 METHODS
68  
69 =cut
70  
71 # }}}1
72  
73 # Internal helpers {{{1
74  
75 # Defined-or
76 40 ▶sub _dor { (defined $_[0]) ? $_[0] : $_[1] }
Subroutine _dor: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 13 27 40
77  
78 # }}}1
79  
80 =head2 new
81  
82 Constructor. Basic usage options:
83  
84 $list = List::AutoNumbered->new(); # first list item is number 1
85 $list = List::AutoNumbered->new($num); # first list item is $num+1
86 $list = List::AutoNumbered->new(-at => $num); # ditto
87  
88 Each successive element
89 will have the next number, unless you say otherwise (e.g., using
90 L<LSKIP()|/LSKIP>). Specifically, the first item in the list will be numbered
91 one higher than the number passed to the C<List::AutoNumbered> constructor.
92  
93 Constructor parameters are processed using L<Getargs::Mixed>, so positional
94 and named parameters are both OK.
95  
96 =head3 The C<how> function
97  
98 You can give the constructor a "how" function that will make the list entry
99 for a single L<load()|/load> or L<add()|/add> call:
100  
101 $list = List::AutoNumbered->new(-how => sub { @_ });
102 # Jam everything together to make a flat array
103 $list = List::AutoNumbered->new(41, sub { @_ });
104 # Positional is OK, too.
105  
106 The C<how> function is called as C<how($num, @data)>. C<$num> is the
107 line number for L<load()|/load> calls, or C<undef> for L<add()|/add> calls.
108 C<@data> is whatever data you passed to C<load()> or C<add()>. For example,
109 the default C<how> function is:
110  
111 sub how {
112 shift unless defined $_[0]; # add passes undef as the line number.
113 [@_] # Wrap everything in an arrayref.
114 }
115  
116 See C<t/05-custom-list-entry.t> for examples of custom C<how> functions.
117  
118 =cut
119  
120 20 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
121 20 my ($class, %args) = parameters('self',[qw(; at how)], @_);
122 my $self = bless {
123 num => _dor($args{at}, 0), # The last number used
124 arr => [], # The data
125 how => _dor($args{how}, # Routine to make a list item
126 31 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
127 31 ▶ shift unless defined $_[0]; # add passes undef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 4 27 31
128 # as the line number.
129 31 [@_] # By default, just wrap it
130 }
131 20 ),
132 }, $class;
133  
134 # Make a loader that adds an item and returns itself --- not $self.
135 # Note that $self is captured --- the loader function does not take
136 # a $self argument.
137 20 ▶ $self->{loader} = sub { $self->_L(@_); return $self->{loader} };
Subroutine __ANON__: called CC=1 SCAR=0.0
138  
139 20 ▶ print "# Created - now $self->{num}\n" if $TRACE;
Branch: 1 branch
BranchTrueFalseTotal
if $List::AutoNumbered::TRACE 3 17 20
140 20 return $self;
141 } #new()
142  
143 # Accessors {{{1
144  
145 =head2 size
146  
147 Returns the size of the array. Like C<scalar @arr>.
148  
149 =cut
150  
151 19 ▶sub size { scalar @{ shift->{arr} }; }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
152  
153 =head2 last
154  
155 Returns the index of the last element in the array. Like C<$#array>.
156  
157 =cut
158  
159 8 ▶sub last { shift->size-1; }
Subroutine last: called CC=1 SCAR=0.0
Pod documented
160  
161 =head2 arr
162  
163 Returns a reference to the array being built. Please do not modify this
164 array directly until you are done loading it. List::AutoNumbered may not
165 work if you do.
166  
167 This can also be called by using the List::AutoNumbered object as an array:
168  
169 my $list = List::AutoNumbered->new...;
170 foreach my $item (@$list) { ... } # Instead of my $item (@{$list->arr})
171  
172 =cut
173  
174 34 ▶sub arr { shift->{arr}; }
Subroutine arr: called CC=1 SCAR=0.0
Pod documented
175  
176 =head2 last_number
177  
178 Returns the current number stored by the instance. This is the number
179 of the most recently preceding L<new()|/new> or L<load()|/load> call.
180 This is B<not> the number that will be given to the next record, since that
181 depends on whether or not the next record has a skip (L<LSKIP()|/LSKIP>).
182  
183 =cut
184  
185 7 ▶sub last_number { shift->{num} }
Subroutine last_number: called CC=1 SCAR=0.0
Pod documented
186  
187 # }}}1
188 # Loading {{{1
189  
190 =head2 load
191  
192 Push a new record with the next number on the front. Usage:
193  
194 $instance->load(whatever args you want to push);
195  
196 Or, if the current record isn't associated with the number immediately after
197 the previous record,
198  
199 $instance->load(LSKIP $n, args);
200  
201 where C<$n> is the number of lines between this C<load()> call and the last one.
202  
203 Returns a coderef that you can call to chain loads. For example, this works:
204  
205 $instance->load(...)->(...)(...)(...) ... ;
206 # You need an arrow ^^ here, but don't need any after that.
207  
208 =cut
209  
210 25 ▶sub load { goto &{ shift->{loader} } } # kick off loading
Subroutine load: called CC=1 SCAR=0.0
Pod documented
211  
212 # _L: Implementation of load()
213 37 ▶sub _L {
Subroutine _L: called CC=2 SCAR=6.9
214 37 my $self = shift;
215  
216 37 ▶ shift if $self->_update_lnum(@_); # Check for skipped lines from LSKIP()
Branch: 1 branch
BranchTrueFalseTotal
if $self->_update_lnum(@_) 11 26 37
217  
218 37 push @{ $self->{arr} }, $self->{how}->($self->{num}, @_);
219 37 return $self;
220 } #_L()
221  
222 =head2 add
223  
224 Add to the array being built, B<without> inserting the number on the front.
225 Does increment the number and respect skips, for consistency.
226  
227 Returns the instance.
228  
229 =cut
230  
231 8 ▶sub add { # just add it
Subroutine add: called CC=2 SCAR=6.9
Pod documented
232 8 my $self = shift;
233 8 ▶ shift if $self->_update_lnum(@_); # Check for skipped lines from LSKIP()
Branch: 1 branch
BranchTrueFalseTotal
if $self->_update_lnum(@_) 1 7 8
234 8 push @{ $self->{arr} }, $self->{how}->(undef, @_);
235 8 return $self;
236 } #add
237  
238 # }}}1
239 # Skipping {{{1
240  
241 =head1 FUNCTIONS
242  
243 =head2 LSKIP
244  
245 A convenience function to create a skipper. Prototyped as C<($)> so you can
246 use it conveniently with L<load()|/load>:
247  
248 $instance->load(LSKIP 1, whatever args...);
249  
250 If you are using line numbers, the parameter to C<LSKIP> should be the number
251 of lines above the current line and below the last L<new()|/new> or
252 L<load()|/load> call. For example:
253  
254 my $instance = List::AutoNumbered->new(__LINE__);
255 # A line
256 # Another one
257 $instance->load(LSKIP 2, # two comment lines between new() and here
258 'some data');
259  
260 =cut
261  
262 19 ▶sub LSKIP ($) {
Subroutine LSKIP: called CC=1 SCAR=0.0
Pod documented
263 19 List::AutoNumbered::Skipper->new(@_);
264 } #LSKIP()
265  
266 # _update_lnum: Increment the line number, and run a skip if there is one.
267 # Call from a method as:
268 # my $self = shift;
269 # shift if $self->_update_lnum(@_);
270  
271 45 ▶sub _update_lnum {
Subroutine _update_lnum: called CC=4 SCAR=13.9
272 45 my $self = shift;
273  
274 45 ▶ if(ref $_[0] eq 'List::AutoNumbered::Skipper') { # implies scalar @_
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'List::AutoNumbered::Skipper') { } 12 33 45
275 12 $self->{num} += $_[0]->{how_many} + 1;
276 12 ▶ print "# Skipped $_[0]->{how_many} - now $self->{num}\n" if $TRACE;
Branch: 1 branch
BranchTrueFalseTotal
if $List::AutoNumbered::TRACE 2 10 12
277 12 return 1; # We do need to shift
278  
279 } else {
280 33 ++$self->{num};
281 33 ▶ print "# Incremented - now $self->{num}\n" if $TRACE;
Branch: 1 branch
BranchTrueFalseTotal
if $List::AutoNumbered::TRACE 4 29 33
282 33 return 0; # No skipper, so don't shift it off.
283 }
284 } #_update_lnum()
285  
286 =head1 INTERNAL PACKAGES
287  
288 =head2 List::AutoNumbered::Skipper
289  
290 This package represents a skip and is created by L<LSKIP()|/LSKIP>.
291 No user-serviceable parts inside.
292  
293 =cut
294  
295 {
296 package List::AutoNumbered::Skipper;
297 6 ▶ use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
298  
299 =head3 new
300  
301 Creates a new skipper. Parameters are for internal use only and are not
302 part of the public API.
303  
304 =cut
305  
306 24 ▶ sub new {
Subroutine new: called CC=3 SCAR=11.0
307 24 my $class = shift;
308 24 ▶ die "Need a single number" unless @_==1 and looks_like_number $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 and &looks_like_number($_[0]) 10 14 24
Condition100%3/3
@_ == 1 and &looks_like_number($_[0])
!ll&&!rl&&r
4614
MC/DC100%2/2
@_ == 1 and &looks_like_number($_[0])
@_ == 1 &looks_like_number($_[0])
Truth table100%3/3
A and B
A= @_ == 1B= &looks_like_number($_[0])
ABresult
0X0
100
111
309 14 bless {how_many => $_[0]}, $class;
310 }
311 } #List::AutoNumbered::Skipper
312  
313 # }}}1
314  
315 6 1; # End of List::AutoNumbered
316  
317 # Rest of the docs {{{1
318 __END__
↑ Summary