File Coverage

blib/lib/SBOM/CycloneDX/List.pm
Criterion Covered Total %
statement 26 54 48.1
branch 1 2 50.0
condition 0 2 0.0
subroutine 11 23 47.8
pod 18 18 100.0
total 56 99 56.5


line stmt bran cond sub pod time code
1             package SBOM::CycloneDX::List;
2              
3 16     16   339 use 5.016000;
  16         131  
4 16     16   97 use strict;
  16         34  
  16         524  
5 16     16   74 use warnings;
  16         43  
  16         899  
6 16     16   152 use utf8;
  16         45  
  16         103  
7              
8 16     16   1102 use overload '@{}' => \&to_array, fallback => 1;
  16         35  
  16         200  
9              
10             sub new {
11 2046     2046 1 3880 my $collection = CORE::shift;
12 2046         45459 bless({collections => [@_]}, $collection);
13             }
14              
15             sub each {
16              
17 12     12 1 109 my ($self, $callback) = @_;
18              
19 12 50       47 return @{$self} unless $callback;
  12         43  
20              
21 0         0 my $idx = 0;
22 0         0 $_->$callback($idx++) for @{$self};
  0         0  
23              
24 0         0 return $self;
25              
26             }
27              
28             sub grep {
29 0     0 1 0 my ($self, $callback) = @_;
30 0         0 return $self->new(CORE::grep { $_->$callback(@_) } @{$self});
  0         0  
  0         0  
31             }
32              
33             sub map {
34 0     0 1 0 my ($self, $callback) = @_;
35 0         0 return $self->new(CORE::map { $_->$callback(@_) } @{$self});
  0         0  
  0         0  
36             }
37              
38 0     0 1 0 sub size { CORE::scalar @{$_[0]->{collections}} }
  0         0  
39              
40 0     0 1 0 sub get { $_[0]->{collections}->[$_[1]] }
41              
42 0     0 1 0 sub set { $_[0]->{collections}->[$_[1]] = $_[2] }
43              
44 0     0 1 0 sub clear { @{$_[0]->{collections}} = () }
  0         0  
45              
46 0     0 1 0 sub pop { CORE::pop @{$_[0]->{collections}} }
  0         0  
47              
48 73     73 1 1210 sub push { CORE::push @{$_[0]->{collections}}, @_[1 .. $#_] }
  73         515  
49              
50 0     0 1 0 sub shift { CORE::shift @{$_[0]->{collections}} }
  0         0  
51              
52 0     0 1 0 sub unshift { CORE::unshift @{$_[0]->{collections}}, @_[1 .. $#_] }
  0         0  
53              
54 0   0 0 1 0 sub join { CORE::join($_[1] // '', @{$_[0]}) }
  0         0  
55              
56 0     0 1 0 sub first { $_[0]->{collections}[0] }
57              
58 0     0 1 0 sub last { $_[0]->{collections}[-1] }
59              
60 17789     17789 1 160359 sub to_array { [@{$_[0]->{collections}}] }
  17789         123664  
61              
62 415     415 1 26711 sub TO_JSON { [@{$_[0]->{collections}}] }
  415         2994  
63              
64             # Aliases
65              
66 48     48 1 5819 sub add { $_[0]->push(@_[1 .. $#_]) }
67              
68             1;
69              
70             =encoding utf-8
71              
72             =head1 NAME
73              
74             SBOM::CycloneDX::List - Collection utility
75              
76             =head1 SYNOPSIS
77              
78             use SBOM::CycloneDX::List;
79             my $collection = SBOM::CycloneDX::List->new( qw[foo bar baz] );
80              
81              
82             =head1 DESCRIPTION
83              
84             L is a collection utility.
85              
86              
87             =head2 METHODS
88              
89             =over
90              
91             =item SBOM::CycloneDX::List->new( ARRAY )
92              
93             Create a new collection.
94              
95             my $c = SBOM::CycloneDX::List->new( [foo bar baz] );
96              
97             =item $c->add
98              
99             Alias for L.
100              
101             =item $c->each
102              
103             Evaluate callback for each element in collection.
104              
105             foreach my $item ($c->each) {
106             [...]
107             }
108              
109             my $collection = $c->each(sub {...});
110              
111             $c->each(sub {
112             my ($value, $idx) = @_;
113             [...]
114             });
115              
116             =item $c->clear
117              
118             Reset the collection.
119              
120             =item $c->first
121              
122             Get the first element of collection.
123              
124             =item $c->get
125              
126             Get item from N index position.
127              
128             my $item = $c->get(5);
129              
130             =item $c->grep
131              
132             Filter items.
133              
134             my $filtered = $c->grep(sub { $_ eq 'foo' });
135              
136             =item $c->push
137              
138             Add a new item in collection.
139              
140             $c->push('foo');
141             $c->push(sub {...});
142              
143             =item $c->join
144              
145             Join elements in collection.
146              
147             $c->join(', ');
148              
149             =item $c->last
150              
151             Get the last element of collection.
152              
153             =item $c->map
154              
155             Evaluate the callback and create a new collection.
156              
157             SBOM::CycloneDX::List->new(1,2,3)->map(sub { $_ * 2 });
158              
159             =item $c->pop
160              
161             Remove and return the last element of collection.
162              
163             my $item = $c->pop;
164              
165             =item $c->push
166              
167             Add one or more elements in collection.
168              
169             $c->push('foo', 'bar', 'baz');
170              
171             =item $c->set
172              
173             Set item value in N index position.
174              
175             $c->set(5, 'foo');
176              
177             =item $c->shift
178              
179             Take the first element from the collection-
180              
181             my $item = $c->shift;
182              
183             =item $c->size
184              
185             Number of item elements.
186              
187             =item $c->unshift
188              
189             Add one or more elements at the beginning of the collection.
190              
191             $c->unshift('baz');
192              
193             =item $c->to_array
194              
195             Return the collection ARRAY.
196              
197             =item $c->TO_JSON
198              
199             Convert the collenction in JSON.
200              
201             =back
202              
203              
204             =head1 SUPPORT
205              
206             =head2 Bugs / Feature Requests
207              
208             Please report any bugs or feature requests through the issue tracker
209             at L.
210             You will be notified automatically of any progress on your issue.
211              
212             =head2 Source Code
213              
214             This is open source software. The code repository is available for
215             public review and contribution under the terms of the license.
216              
217             L
218              
219             git clone https://github.com/giterlizzi/perl-SBOM-CycloneDX.git
220              
221              
222             =head1 AUTHOR
223              
224             =over 4
225              
226             =item * Giuseppe Di Terlizzi
227              
228             =back
229              
230              
231             =head1 LICENSE AND COPYRIGHT
232              
233             This software is copyright (c) 2025-2026 by Giuseppe Di Terlizzi.
234              
235             This is free software; you can redistribute it and/or modify it under
236             the same terms as the Perl 5 programming language system itself.
237              
238             =cut