lib/File/URIList.pm

filter: Statementstmt 21.9% 29 / 132 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 50.0% 9 / 18 Podpod 100% 7 / 7 totaltotal 16.4% 45 / 273 CC 54 SCAR 60.6
CC
54
Cov
12%
read_to
930.0
write_list
132.0
new
90.0
CRAP 427.5
↑ Summary blib/lib/File/URIList.pm »
1 # Copyright (c) 2025 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 1 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Fcntl qw(SEEK_SET);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use URI::file;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use parent qw(Data::Identifier::Interface::Userdata Data::Identifier::Interface::Subobjects);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 use constant {
20 1 CRLF => "\015\012",
21 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 our $VERSION = v0.04;
24  
25 1 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 sub new {
36 0 ▶ my ($pkg, $handle, %opts) = @_;
Subroutine new: not called CC=9 SCAR=45.0
Pod documented
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 sub write_comment {
67 0 ▶ my ($self, @list) = @_;
Subroutine write_comment: not called CC=5 SCAR=34.0
Pod documented
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 sub write_list {
86 0 ▶ my ($self, @list) = @_;
Subroutine write_list: not called CC=11 SCAR=48.8
Pod documented
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 $ent->read_to(sub {
105 0 ▶ print $fh $_[1]->as_string, CRLF;
Subroutine __ANON__: not called CC=1 SCAR=6.9
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 sub read_to {
118 0 ▶ my ($self, $cb, %opts) = @_;
Subroutine read_to: not called CC=30 SCAR=68.4
Pod documented
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 sub read_as_list {
205 0 ▶ my ($self, %opts) = @_;
Subroutine read_as_list: not called CC=2 SCAR=17.9
Pod documented
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 sub rewind {
223 0 ▶ my ($self) = @_;
Subroutine rewind: not called CC=2 SCAR=17.9
Pod documented
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 sub clear {
232 0 ▶ my ($self) = @_;
Subroutine clear: not called CC=1 SCAR=6.9
Pod documented
233 0 my $fh = $self->{fh};
234  
235 0 $self->rewind;
236  
237 0 $fh->truncate(0);
238 }
239  
240 1 1;
241  
242 __END__
↑ Summary blib/lib/File/URIList.pm »