| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Object::Array::Plugin::Builtins; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 4146 | use strict; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 123 |  | 
| 4 | 3 |  |  | 3 |  | 19 | use warnings; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 152 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 3 |  |  |  |  | 36 | use Sub::Exporter -setup => { | 
| 7 |  |  |  |  |  |  | exports => [ | 
| 8 |  |  |  |  |  |  | qw( | 
| 9 |  |  |  |  |  |  | size length | 
| 10 |  |  |  |  |  |  | element elem | 
| 11 |  |  |  |  |  |  | slice | 
| 12 |  |  |  |  |  |  | elements elems | 
| 13 |  |  |  |  |  |  | clear | 
| 14 |  |  |  |  |  |  | pop | 
| 15 |  |  |  |  |  |  | push | 
| 16 |  |  |  |  |  |  | shift | 
| 17 |  |  |  |  |  |  | unshift | 
| 18 |  |  |  |  |  |  | exists | 
| 19 |  |  |  |  |  |  | delete | 
| 20 |  |  |  |  |  |  | splice | 
| 21 |  |  |  |  |  |  | map | 
| 22 |  |  |  |  |  |  | grep | 
| 23 |  |  |  |  |  |  | join | 
| 24 |  |  |  |  |  |  | ), | 
| 25 |  |  |  |  |  |  | ], | 
| 26 | 3 |  |  | 3 |  | 15 | }; | 
|  | 3 |  |  |  |  | 6 |  | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | =head1 NAME | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | Object::Array::Plugin::Builtins | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | See L. | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | Provides analogues to Perl's built-in array operations. | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | =head1 METHODS | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | =head2 C<< size >> | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | =head2 C<< length >> | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | Returns the number of elements in the array. | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | C<< size >> and C<< length >> are synonyms. | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =head2 C<< element >> | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | =head2 C<< elem >> | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | print $array->elem(0); | 
| 53 |  |  |  |  |  |  | print $array->[0]; | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | Get a single element's value. | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | $array->elem(1 => "hello"); | 
| 58 |  |  |  |  |  |  | $array->[1] = "hello"; | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | Set a single element's value. | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | C<< element >> and C<< elem >> are synonyms. | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | =head2 C<< slice >> | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | print for $array->slice([ 0, 1, 2 ]); | 
| 67 |  |  |  |  |  |  | print for @{$array}[0,1,2]; | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | Get multiple values. | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | $array->slice([ 0, 1, 2 ] => [ qw(a b c) ]); | 
| 72 |  |  |  |  |  |  | @{$array}[0,1,2] = qw(a b c); | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | Set multiple values. | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | =head2 C<< elements >> | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | =head2 C<< elems >> | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | Shortcut for all values in the array. | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | C<< elements >> and C<< elems >> are synonyms. | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | NOTE: Using methods in a for/map/etc. will not do aliasing | 
| 85 |  |  |  |  |  |  | via $_.  Use array dereferencing if you need to do this, e.g. | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | $_++ for @{$array}; | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =head2 C<< clear >> | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | Erase the array.  The following all leave the array empty: | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | $array->size(0); | 
| 94 |  |  |  |  |  |  | $array->clear; | 
| 95 |  |  |  |  |  |  | @{ $array } = (); | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | =head2 C<< push >> | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | =head2 C<< pop >> | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | =head2 C<< shift >> | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | =head2 C<< unshift >> | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | =head2 C<< exists >> | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | =head2 C<< delete >> | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | =head2 C<< splice >> | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | =head2 C<< map >> | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =head2 C<< grep >> | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | =head2 C<< join >> | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | As the builtin array operations of the same names. | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | Note that since map and grep are called as methods, you must | 
| 120 |  |  |  |  |  |  | use C<> (no bare blocks). | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =cut | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | sub map { | 
| 125 | 1 |  |  | 1 | 1 | 687 | my ($self, $code) = @_; | 
| 126 | 1 |  |  |  |  | 2 | return $self->_array(map { $code->() } @{ $self }); | 
|  | 2 |  |  |  |  | 9 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | sub grep { | 
| 130 | 2 |  |  | 2 | 1 | 5 | my ($self, $code) = @_; | 
| 131 | 2 |  |  |  |  | 2 | return $self->_array(grep { $code->() } @{ $self }); | 
|  | 6 |  |  |  |  | 23 |  | 
|  | 2 |  |  |  |  | 8 |  | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | sub join { | 
| 135 | 2 |  |  | 2 | 1 | 6 | my $self = shift; | 
| 136 | 2 |  |  |  |  | 3 | return join(shift, @{ $self }); | 
|  | 2 |  |  |  |  | 6 |  | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | sub size { | 
| 140 | 5 |  |  | 5 | 1 | 457 | my $self = shift; | 
| 141 | 5 | 50 |  |  |  | 16 | if (@_) { | 
| 142 | 0 |  |  |  |  | 0 | $#{ $self->_real } = shift(@_) - 1; | 
|  | 0 |  |  |  |  | 0 |  | 
| 143 |  |  |  |  |  |  | } | 
| 144 | 5 |  |  |  |  | 7 | return scalar @{ $self->_real }; | 
|  | 5 |  |  |  |  | 15 |  | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | *length = \*size; | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | sub elem { | 
| 150 | 2 |  |  | 2 | 1 | 9 | my $self = shift; | 
| 151 | 2 | 50 |  |  |  | 7 | unless (@_) { | 
| 152 | 0 |  |  |  |  | 0 | require Carp; | 
| 153 | 0 |  |  |  |  | 0 | Carp::croak("must specify index for element lookup"); | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 2 |  | 50 |  |  | 5 | my $idx  = shift || 0; | 
| 157 |  |  |  |  |  |  |  | 
| 158 | 2 | 50 |  |  |  | 7 | if (@_) { | 
| 159 | 0 |  |  |  |  | 0 | $self->_real->[$idx] = shift; | 
| 160 |  |  |  |  |  |  | } | 
| 161 | 2 |  |  |  |  | 6 | return $self->_real->[$idx]; | 
| 162 |  |  |  |  |  |  | } | 
| 163 |  |  |  |  |  |  | *element = \&elem; | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | sub slice { | 
| 166 | 2 |  |  | 2 | 1 | 3 | my $self = shift; | 
| 167 | 2 |  |  |  |  | 2 | my $idx  = shift; | 
| 168 | 2 | 50 | 33 |  |  | 15 | unless ($idx and ref($idx) eq 'ARRAY') { | 
| 169 | 0 |  |  |  |  | 0 | require Carp; | 
| 170 | 0 |  |  |  |  | 0 | Carp::croak("must specify arrayref of indices for slice"); | 
| 171 |  |  |  |  |  |  | } | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | # since tying can deal with this, might as well let it | 
| 174 | 2 | 100 |  |  |  | 6 | if (@_) { | 
| 175 | 1 |  |  |  |  | 3 | return $self->_array(@{ $self }[ @$idx ] = @{ +shift }); | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 2 |  | 
| 176 |  |  |  |  |  |  | } else { | 
| 177 | 1 |  |  |  |  | 2 | return $self->_array(@{ $self }[ @$idx ]); | 
|  | 1 |  |  |  |  | 4 |  | 
| 178 |  |  |  |  |  |  | } | 
| 179 |  |  |  |  |  |  | } | 
| 180 |  |  |  |  |  |  |  | 
| 181 | 31 |  |  | 31 | 1 | 731 | sub elems   { @{ shift->_real } } | 
|  | 31 |  |  |  |  | 85 |  | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | *elements = \&elems; | 
| 184 |  |  |  |  |  |  |  | 
| 185 | 0 |  |  | 0 | 1 | 0 | sub clear   { @{ shift->_real } = () } | 
|  | 0 |  |  |  |  | 0 |  | 
| 186 |  |  |  |  |  |  |  | 
| 187 | 2 |  |  | 2 | 1 | 4 | sub pop     { pop @{ shift->_real } } | 
|  | 2 |  |  |  |  | 9 |  | 
| 188 |  |  |  |  |  |  |  | 
| 189 | 2 |  |  | 2 | 1 | 4 | sub push    { push @{ shift->_real }, @_ } | 
|  | 2 |  |  |  |  | 7 |  | 
| 190 |  |  |  |  |  |  |  | 
| 191 | 0 |  |  | 0 | 1 | 0 | sub unshift { unshift @{ shift->_real }, @_ } | 
|  | 0 |  |  |  |  | 0 |  | 
| 192 |  |  |  |  |  |  |  | 
| 193 | 1 |  |  | 1 | 1 | 9 | sub exists  { exists shift->_real->[shift] } | 
| 194 |  |  |  |  |  |  |  | 
| 195 | 1 |  |  | 1 | 1 | 7 | sub delete  { delete shift->_real->[shift] } | 
| 196 |  |  |  |  |  |  |  | 
| 197 | 0 |  |  | 0 | 1 | 0 | sub splice  { splice @{ shift->_real }, @_ } | 
|  | 0 |  |  |  |  | 0 |  | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | # shift goes last to avoid annoying warnings | 
| 200 | 1 |  |  | 1 | 1 | 2 | sub shift   { shift @{ shift->_real } } | 
|  | 1 |  |  |  |  | 5 |  | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | 1; |