lib/File/URIList.pm

filter: Statementstmt 26.2% 37 / 141 Branchbran 4.6% 4 / 86 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 52.6% 10 / 19 Podpod 100% 8 / 8 totaltotal 20.3% 59 / 290 CC 57 SCAR 60.0
CC
57
Cov
16%
read_to
930.0
write_list
132.0
new
90.0
CRAP 404.9
↑ Summary
1 # Copyright (c) 2025-2026 Philipp Schafft
2  
3 # licensed under Artistic License 2.0 (see LICENSE file)
4  
5 # ABSTRACT: module for reading and writing RFC 2483 URI lists
6  
7 package File::URIList;
8  
9 2 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Fcntl qw(SEEK_SET);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use URI::file;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use parent qw(Data::Identifier::Interface::Userdata Data::Identifier::Interface::Subobjects);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 CRLF => "\015\012",
21 2 };
22  
23 2 our $VERSION = v0.05;
24  
25 2 my %_check_defaults = (
26 blank_lines => 'die',
27 extra_spaces => 'die',
28 no_scheme => 'die',
29 slash_as_local => undef,
30 );
31  
32  
33  
34 #@returns __PACKAGE__
35 0 ▶sub new {
Subroutine new: not called CC=9 SCAR=45.0
Pod documented
36 0 my ($pkg, $handle, %opts) = @_;
37 0 my %check = %_check_defaults;
38 0 my $self = bless {check => \%check}, $pkg;
39  
40 0 foreach my $key (keys %_check_defaults) {
41 0 ▶ if (exists $opts{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{$key}) 0 0 0
42 0 $check{$key} = delete $opts{$key};
43 }
44 }
45  
46 0 ▶ if (defined(my $baseuri = delete $opts{baseuri})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $baseuri = delete $opts{"baseuri"})) 0 0 0
47 0 ▶ $baseuri = URI->new($baseuri) unless eval { $baseuri->isa('URI') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $baseuri->isa("URI") } } 0 0 0
48 0 $self->{baseuri} = $baseuri;
49 }
50  
51 0 ▶ croak 'Stray options passed' if scalar keys %opts;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %opts 0 0 0
52  
53 0 ▶ unless (ref $handle) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $handle) 0 0 0
54 0 ▶ open(my $fh, '<', $handle) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $handle or die $! 0 0 0
55 0 $handle = $fh;
56 }
57  
58 0 ▶ binmode($handle) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
binmode $handle or die $! 0 0 0
59  
60 0 $self->{fh} = $handle;
61  
62 0 return $self;
63 }
64  
65  
66 0 ▶sub write_comment {
Subroutine write_comment: not called CC=5 SCAR=34.0
Pod documented
67 0 my ($self, @list) = @_;
68 0 my $fh = $self->{fh};
69  
70 0 foreach my $ent (@list) {
71 0 ▶ if (ref($ent) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ent eq 'ARRAY') { } 0 0 0
elsif (ref $ent) { } 0 0 0
72 0 $self->write_comment(@{$ent});
73 } elsif (ref($ent)) {
74 0 print $fh '# ';
75 0 $self->write_list($ent);
76 } else {
77 0 foreach my $line (split/\015?\012/, $ent) {
78 0 print $fh '# ', $line, CRLF;
79 }
80 }
81 }
82 }
83  
84  
85 0 ▶sub write_list {
Subroutine write_list: not called CC=11 SCAR=48.8
Pod documented
86 0 my ($self, @list) = @_;
87 0 my $fh = $self->{fh};
88  
89 0 foreach my $ent (@list) {
90 0 ▶ if (ref($ent) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ent eq 'ARRAY') { } 0 0 0
elsif (ref $ent) { } 0 0 0
91 0 $self->write_list(@{$ent});
92 } elsif (ref($ent)) {
93 0 ▶ if ($ent->isa('URI')) {
Branch: 6 branches
BranchTrueFalseTotal
if ($ent->isa('URI')) { } 0 0 0
elsif ($ent->isa('Data::Identifier')) { } 0 0 0
elsif ($ent->isa('Data::URIID::Result')) { } 0 0 0
elsif ($ent->isa('Data::URIID::Base') or $ent->isa('Data::Identifier::Interface::Simple')) { } 0 0 0
elsif ($ent->isa('Data::Identifier::Cloudlet')) { } 0 0 0
elsif ($ent->isa(__PACKAGE__)) { } 0 0 0
94 0 print $fh $ent->as_string, CRLF;
95 } elsif ($ent->isa('Data::Identifier')) {
96 0 print $fh $ent->uri, CRLF;
97 } elsif ($ent->isa('Data::URIID::Result')) {
98 0 print $fh $ent->url, CRLF;
99 ▶ } elsif ($ent->isa('Data::URIID::Base') || $ent->isa('Data::Identifier::Interface::Simple')) {
Condition0%0/3
$ent->isa("Data::URIID::Base") or $ent->isa("Data::Identifier::Interface::Simple")
l!l&&r!l&&!r
000
MC/DC0%0/2
$ent->isa("Data::URIID::Base") or $ent->isa("Data::Identifier::Interface::Simple")
$ent->isa("Data::URIID::Base") $ent->isa("Data::Identifier::Interface::Simple")
Truth table0%0/3
A or B
A= $ent->isa("Data::URIID::Base")B= $ent->isa("Data::Identifier::Interface::Simple")
ABresult
1X1
011
000
100 0 print $fh $ent->as('uri'), CRLF;
101 } elsif ($ent->isa('Data::Identifier::Cloudlet')) {
102 0 $self->write_list($ent->roots);
103 } elsif ($ent->isa(__PACKAGE__)) {
104 0 ▶ $ent->read_to(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
105 0 print $fh $_[1]->as_string, CRLF;
106 0 });
107 } else {
108 0 croak 'Unsupported object passed';
109 }
110 } else {
111 0 print $fh URI->new($ent)->as_string, CRLF;
112 }
113 }
114 }
115  
116  
117 0 ▶sub read_to {
Subroutine read_to: not called CC=30 SCAR=68.4
Pod documented
118 0 my ($self, $cb, %opts) = @_;
119 0 my $fh = $self->{fh};
120 0 ▶ my $as = delete($opts{as}) // 'URI';
Condition0%0/2
delete $opts{"as"} // "URI"
l!l
00
MC/DC0%0/1
delete $opts{"as"} // "URI"
delete $opts{"as"}
Truth table0%0/2
A
A= delete $opts{"as"}
Aresult
11
00
121  
122 0 ▶ croak 'Stray options passed' if scalar keys %opts;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %opts 0 0 0
123  
124 # Preload modules as needed:
125 0 ▶ if ($as ne 'URI') {
Branch: 1 branch
BranchTrueFalseTotal
if ($as ne "URI") 0 0 0
126 0 require Data::Identifier;
127 }
128  
129 0 ▶ if (ref($cb) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cb eq "ARRAY") 0 0 0
130 0 my $list = $cb;
131 0 ▶ $cb = sub { push(@{$list}, $_[1]) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 }
133  
134 0 ▶ while (defined(my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
135 0 $line =~ s/\015?\012$//;
136  
137 0 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/u 0 0 0
138  
139 0 ▶ if ($line =~ /^\s/ || $line =~ /\s$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s/u or $line =~ /\s$/u) 0 0 0
Condition0%0/3
$line =~ /^\s/u or $line =~ /\s$/u
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^\s/u or $line =~ /\s$/u
$line =~ /^\s/u $line =~ /\s$/u
Truth table0%0/3
A or B
A= $line =~ /^\s/uB= $line =~ /\s$/u
ABresult
1X1
011
000
140 0 ▶ my $action = $self->{check}{extra_spaces} // '';
Condition0%0/2
$self->{"check"}{"extra_spaces"} // ""
l!l
00
MC/DC0%0/1
$self->{"check"}{"extra_spaces"} // ""
$self->{"check"}{"extra_spaces"}
Truth table0%0/2
A
A= $self->{"check"}{"extra_spaces"}
Aresult
11
00
141  
142 0 ▶ if ($action eq 'die') {
Branch: 3 branches
BranchTrueFalseTotal
if ($action eq 'die') { } 0 0 0
elsif ($action eq 'trim') { } 0 0 0
elsif ($action eq 'pass') { } 0 0 0
143 0 croak 'Input line with extra spaces, aborting';
144 } elsif ($action eq 'trim') {
145 0 $line =~ s/^\s+//;
146 0 $line =~ s/\s+$//;
147 } elsif ($action eq 'pass') {
148 # no-op
149 } else {
150 0 croak 'Input line with extra spaces and bad handling action selected: '.$action;
151 }
152 }
153  
154 0 ▶ if ($line eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "") 0 0 0
155 0 ▶ my $action = $self->{check}{blank_lines} // '';
Condition0%0/2
$self->{"check"}{"blank_lines"} // ""
l!l
00
MC/DC0%0/1
$self->{"check"}{"blank_lines"} // ""
$self->{"check"}{"blank_lines"}
Truth table0%0/2
A
A= $self->{"check"}{"blank_lines"}
Aresult
11
00
156  
157 0 ▶ if ($action eq 'die') {
Branch: 4 branches
BranchTrueFalseTotal
if ($action eq 'die') { } 0 0 0
elsif ($action eq 'skip') { } 0 0 0
elsif ($action eq 'undef') { } 0 0 0
elsif ($action eq 'pass') { } 0 0 0
158 0 croak 'Blank line in input, aborting';
159 } elsif ($action eq 'skip') {
160 0 next;
161 } elsif ($action eq 'undef') {
162 0 $cb->($self, undef);
163 0 next;
164 } elsif ($action eq 'pass') {
165 # no-op
166 } else {
167 0 croak 'Blank line in input and bad handling action selected: '.$action;
168 }
169 }
170  
171 0 ▶ if ($line =~ /^\// && $self->{check}{slash_as_local}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[^/]u and $self->{"check"}{"slash_as_local"}) 0 0 0
Condition0%0/3
$line =~ m[^/]u and $self->{"check"}{"slash_as_local"}
!ll&&!rl&&r
000
MC/DC0%0/2
$line =~ m[^/]u and $self->{"check"}{"slash_as_local"}
$line =~ m[^/]u $self->{"check"}{"slash_as_local"}
Truth table0%0/3
A and B
A= $line =~ m[^/]uB= $self->{"check"}{"slash_as_local"}
ABresult
0X0
100
111
172 0 $line = URI::file->new($line);
173 }
174  
175 0 ▶ if (!ref($line) && !defined($self->{baseuri}) && $line !~ /^[a-zA-Z][a-zA-Z0-9\+\.\-]+:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $line or defined $self->{"baseuri"} or $line =~ /^[a-zA-Z][a-zA-Z0-9\+\.\-]+:/u) 0 0 0
Condition0%0/6
ref $line or defined $self->{"baseuri"} or $line =~ /^[a-zA-Z][a-zA-Z0-9\+\.\-]+:/u
l!l&&r!l&&!r
000
ref $line or defined $self->{"baseuri"}
l!l&&r!l&&!r
000
MC/DC0%0/3
ref $line or defined $self->{"baseuri"} or $line =~ /^[a-zA-Z][a-zA-Z0-9\+\.\-]+:/u
ref $line defined $self->{"baseuri"} $line =~ /^[a-zA-Z][a-zA-Z0-9\+\.\-]+:/u
Truth table0%0/4
A or B or C
A= ref $lineB= defined $self->{"baseuri"}C= $line =~ /^[a-zA-Z][a-zA-Z0-9\+\.\-]+:/u
ABCresult
1XX1
01X1
0011
0000
176 0 ▶ my $action = $self->{check}{no_scheme} // '';
Condition0%0/2
$self->{"check"}{"no_scheme"} // ""
l!l
00
MC/DC0%0/1
$self->{"check"}{"no_scheme"} // ""
$self->{"check"}{"no_scheme"}
Truth table0%0/2
A
A= $self->{"check"}{"no_scheme"}
Aresult
11
00
177  
178 0 ▶ if ($action eq 'die') {
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq 'die') { } 0 0 0
elsif ($action eq 'pass') { } 0 0 0
179 0 croak 'URI with no scheme, aborting';
180 } elsif ($action eq 'pass') {
181 # no-op
182 } else {
183 0 croak 'URI with no scheme and bad handling action selected: '.$action;
184 }
185 }
186  
187 0 ▶ unless (ref $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $line) 0 0 0
188 0 ▶ if (defined $self->{baseuri}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'baseuri'}) { } 0 0 0
189 0 $line = URI->new_abs($line, $self->{baseuri});
190 } else {
191 0 $line = URI->new($line);
192 }
193 }
194  
195 0 ▶ if ($as ne 'URI') {
Branch: 1 branch
BranchTrueFalseTotal
if ($as ne "URI") 0 0 0
196 0 $line = $line->Data::Identifier::as($as);
197 }
198  
199 0 $cb->($self, $line);
200 }
201 }
202  
203  
204 0 ▶sub read_as_list {
Subroutine read_as_list: not called CC=2 SCAR=17.9
Pod documented
205 0 my ($self, %opts) = @_;
206 0 my $list = [];
207 0 my $listas = delete $opts{listas};
208  
209 0 delete $opts{list}; # we are always in list mode
210  
211 0 $self->read_to($list, %opts);
212  
213 0 ▶ if (defined($listas)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $listas) 0 0 0
214 0 require Data::Identifier::Cloudlet;
215 0 $list = Data::Identifier::Cloudlet->new(root => $list)->as($listas);
216 }
217  
218 0 return $list;
219 }
220  
221  
222 0 ▶sub rewind {
Subroutine rewind: not called CC=2 SCAR=17.9
Pod documented
223 0 my ($self) = @_;
224 0 my $fh = $self->{fh};
225  
226 0 ▶ $fh->seek(0, SEEK_SET) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
$fh->seek(0, 0) or die $! 0 0 0
227 0 $fh->input_line_number(1);
228 }
229  
230  
231 0 ▶sub clear {
Subroutine clear: not called CC=1 SCAR=6.9
Pod documented
232 0 my ($self) = @_;
233 0 my $fh = $self->{fh};
234  
235 0 $self->rewind;
236  
237 0 $fh->truncate(0);
238 }
239  
240  
241 2 ▶sub media_subtype {
Subroutine media_subtype: called CC=4 SCAR=14.2
Pod documented
242 2 my ($pkg, $as, @opts) = @_;
243  
244 2 ▶ croak 'Stray options passed' if scalar keys @opts;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys @opts 0 2 2
245  
246 2 ▶ if (defined $as) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $as) 1 1 2
247 1 require Data::Identifier;
248 1 state $id = Data::Identifier->new(
249 uuid => 'ceecde0d-4fbe-595f-92a5-b792692d341f',
250 tagname => 'text/uri-list',
251 generator => Data::Identifier->new(uuid => 'a649d48d-35b0-4454-81af-c5fd2eb40373')->register,
252 request => 'text/uri-list',
253 )->register;
254  
255 1 ▶ return $id if eval {$id->isa($as)};
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $id->isa($as) } } 1 0 1
256 0 return $id->as($as);
257 }
258  
259 1 return 'text/uri-list';
260 }
261  
262 2 1;
263  
264 __END__
↑ Summary