lib/File/URIList.pm

filter: Statementstmt 66.6% 94 / 141 Branchbran 36.0% 31 / 86 Conditioncond 21.7% 5 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 89.4% 17 / 19 Podpod 100% 8 / 8 totaltotal 53.4% 155 / 290 CC 57 SCAR 46.7
CC
57
Cov
52%
read_to
243.5
write_list
45.3
new
18.2
CRAP 107.2
↑ 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 3 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use Fcntl qw(SEEK_SET);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use URI::file;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use parent qw(Data::Identifier::Interface::Userdata Data::Identifier::Interface::Subobjects);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 CRLF => "\015\012",
21 3 };
22  
23 3 our $VERSION = v0.06;
24  
25 3 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 1 ▶sub new {
Subroutine new: called CC=9 SCAR=29.0
Pod documented
36 1 my ($pkg, $handle, %opts) = @_;
37 1 my %check = %_check_defaults;
38 1 my $self = bless {check => \%check}, $pkg;
39  
40 1 foreach my $key (keys %_check_defaults) {
41 4 ▶ if (exists $opts{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{$key}) 0 4 4
42 0 $check{$key} = delete $opts{$key};
43 }
44 }
45  
46 1 ▶ if (defined(my $baseuri = delete $opts{baseuri})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $baseuri = delete $opts{"baseuri"})) 0 1 1
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 1 ▶ croak 'Stray options passed' if scalar keys %opts;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %opts 0 1 1
52  
53 1 ▶ unless (ref $handle) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $handle) 0 1 1
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 1 ▶ binmode($handle) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
binmode $handle or die $! 1 0 1
59  
60 1 $self->{fh} = $handle;
61  
62 1 return $self;
63 }
64  
65  
66 1 ▶sub write_comment {
Subroutine write_comment: called CC=5 SCAR=19.4
Pod documented
67 1 my ($self, @list) = @_;
68 1 my $fh = $self->{fh};
69  
70 1 foreach my $ent (@list) {
71 1 ▶ if (ref($ent) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ent eq 'ARRAY') { } 0 1 1
elsif (ref $ent) { } 0 1 1
72 0 $self->write_comment(@{$ent});
73 } elsif (ref($ent)) {
74 0 print $fh '# ';
75 0 $self->write_list($ent);
76 } else {
77 1 foreach my $line (split/\015?\012/, $ent) {
78 1 print $fh '# ', $line, CRLF;
79 }
80 }
81 }
82 }
83  
84  
85 1 ▶sub write_list {
Subroutine write_list: called CC=11 SCAR=38.1
Pod documented
86 1 my ($self, @list) = @_;
87 1 my $fh = $self->{fh};
88  
89 1 foreach my $ent (@list) {
90 2 ▶ if (ref($ent) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ent eq 'ARRAY') { } 0 2 2
elsif (ref $ent) { } 1 1 2
91 0 $self->write_list(@{$ent});
92 } elsif (ref($ent)) {
93 1 ▶ if ($ent->isa('URI')) {
Branch: 6 branches
BranchTrueFalseTotal
if ($ent->isa('URI')) { } 0 1 1
elsif ($ent->isa('Data::Identifier')) { } 1 0 1
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 1 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 1 print $fh URI->new($ent)->as_string, CRLF;
112 }
113 }
114 }
115  
116  
117 1 ▶sub read_to {
Subroutine read_to: called CC=30 SCAR=55.0
Pod documented
118 1 my ($self, $cb, %opts) = @_;
119 1 my $fh = $self->{fh};
120 1 ▶ my $as = delete($opts{as}) // 'URI';
Condition50%1/2
delete $opts{"as"} // "URI"
l!l
01
MC/DC0%0/1
delete $opts{"as"} // "URI"
delete $opts{"as"}
Truth table50%1/2
A
A= delete $opts{"as"}
Aresult
11
00
121  
122 1 ▶ croak 'Stray options passed' if scalar keys %opts;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %opts 0 1 1
123  
124 # Preload modules as needed:
125 1 ▶ if ($as ne 'URI') {
Branch: 1 branch
BranchTrueFalseTotal
if ($as ne "URI") 0 1 1
126 0 require Data::Identifier;
127 }
128  
129 1 ▶ if (ref($cb) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cb eq "ARRAY") 1 0 1
130 1 my $list = $cb;
131 1 ▶ $cb = sub { push(@{$list}, $_[1]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
132 }
133  
134 1 ▶ while (defined(my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 2 1 3
135 2 $line =~ s/\015?\012$//;
136  
137 2 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/u 0 2 2
138  
139 2 ▶ if ($line =~ /^\s/ || $line =~ /\s$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s/u or $line =~ /\s$/u) 0 2 2
Condition33%1/3
$line =~ /^\s/u or $line =~ /\s$/u
l!l&&r!l&&!r
002
MC/DC0%0/2
$line =~ /^\s/u or $line =~ /\s$/u
$line =~ /^\s/u $line =~ /\s$/u
Truth table33%1/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 2 ▶ if ($line eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "") 0 2 2
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 2 ▶ if ($line =~ /^\// && $self->{check}{slash_as_local}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[^/]u and $self->{"check"}{"slash_as_local"}) 0 2 2
Condition33%1/3
$line =~ m[^/]u and $self->{"check"}{"slash_as_local"}
!ll&&!rl&&r
200
MC/DC0%0/2
$line =~ m[^/]u and $self->{"check"}{"slash_as_local"}
$line =~ m[^/]u $self->{"check"}{"slash_as_local"}
Truth table33%1/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 2 ▶ 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 2 2
Condition33%2/6
ref $line or defined $self->{"baseuri"} or $line =~ /^[a-zA-Z][a-zA-Z0-9\+\.\-]+:/u
l!l&&r!l&&!r
020
ref $line or defined $self->{"baseuri"}
l!l&&r!l&&!r
002
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 table25%1/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 2 ▶ unless (ref $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $line) 2 0 2
188 2 ▶ if (defined $self->{baseuri}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'baseuri'}) { } 0 2 2
189 0 $line = URI->new_abs($line, $self->{baseuri});
190 } else {
191 2 $line = URI->new($line);
192 }
193 }
194  
195 2 ▶ if ($as ne 'URI') {
Branch: 1 branch
BranchTrueFalseTotal
if ($as ne "URI") 0 2 2
196 0 $line = $line->Data::Identifier::as($as);
197 }
198  
199 2 $cb->($self, $line);
200 }
201 }
202  
203  
204 1 ▶sub read_as_list {
Subroutine read_as_list: called CC=2 SCAR=7.3
Pod documented
205 1 my ($self, %opts) = @_;
206 1 my $list = [];
207 1 my $listas = delete $opts{listas};
208  
209 1 delete $opts{list}; # we are always in list mode
210  
211 1 $self->read_to($list, %opts);
212  
213 1 ▶ if (defined($listas)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $listas) 0 1 1
214 0 require Data::Identifier::Cloudlet;
215 0 $list = Data::Identifier::Cloudlet->new(root => $list)->as($listas);
216 }
217  
218 1 return $list;
219 }
220  
221  
222 2 ▶sub rewind {
Subroutine rewind: called CC=2 SCAR=7.0
Pod documented
223 2 my ($self) = @_;
224 2 my $fh = $self->{fh};
225  
226 2 ▶ $fh->seek(0, SEEK_SET) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
$fh->seek(0, 0) or die $! 2 0 2
227 2 $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 3 1;
263  
264 __END__
↑ Summary