File Coverage

blib/lib/Array/Slice.pm
Criterion Covered Total %
statement 13 13 100.0
branch 2 2 100.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 21 21 100.0


line stmt bran cond sub pod time code
1             # $Id: Slice.pm,v 1.11 2007/04/16 07:33:29 dk Exp $
2             package Array::Slice;
3              
4 6     6   123000 use strict;
  6         16  
  6         202  
5 6     6   29 use warnings;
  6         10  
  6         167  
6 6     6   5075 use Want qw(howmany);
  6         12651  
  6         940  
7              
8             our $VERSION = '0.03';
9 6     6   46 use base qw(DynaLoader Exporter);
  6         10  
  6         1781  
10              
11             our @EXPORT_OK = qw(reset slice);
12             our %EXPORT_TAGS = ( all => \@EXPORT_OK);
13              
14             bootstrap Array::Slice $VERSION;
15              
16 51 100   51 1 12189 sub slice(\@;$) { array_slice( $_[0], $#_ ? $_[1] : howmany) }
17              
18             1;
19              
20             =pod
21              
22             =head1 NAME
23              
24             Array::Slice - context-dependent array iterator
25              
26             =head1 SYNOPSIS
27              
28             use Array::Slice qw(slice);
29              
30             Iterate over an array assigning several items per cycle. Three:
31              
32             while ( my ( $x, $y, $z) = slice @arr) { ... }
33              
34             or two:
35              
36             while ( my ( undef, $second) = slice @arr) { ... }
37              
38             or even forty-two:
39              
40             while ( @a = slice @arr, 42) { ... }
41              
42             =head1 DESCRIPTION
43              
44             I've always wanted the power of C to be applied to arrays
45             working with more than one item at a time. Perl6 does it, Perl5 with source
46             filtering can do it, close, but no cigar. This module is an small step towards
47             the idea, an attempt to produce a way of slicing a single array with least
48             obtrusive syntax I can think of.
49              
50             The module works by attaching an integer counter to each scalar using perl
51             magic API, advancing the counter on each slice.
52              
53             =over
54              
55             =item slice @array, [ $howmany ]
56              
57             Iterates over an array, returning C<$howmany> items perl call. If called without C<$howmany>,
58             deduces the number depending on the calling context.
59              
60             =item reset @array, [ $whereto ]
61              
62             Resets the array iterator to C<$whereto> or 0. C<$whereto> can be negavtive, as in
63             native array indexing.
64              
65             =back
66              
67             =head1 BUGS
68              
69             Array items are copied, not aliased as in C/C.
70              
71             Doesn't work with lists. This is one big TODO.
72              
73             =head1 SEE ALSO
74              
75             L, L, L, L,
76             Synopsis 04(The 'for' statement).
77              
78             =head1 THANKS
79              
80             Aaron Crane for implementation of L, which code was used as base
81             for this module.
82              
83             =head1 AUTHOR
84              
85             Dmitry Karasik, Edmitry@karasik.eu.orgE.
86              
87             =cut