blib/lib/File/URIList.pm untested

filter: Statementstmt 0.0% 0 / 141 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 29 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 267 CC 37 SCAR 53.0
CC
37
Cov
0%
read_to
462.0
write_list
90.0
new
42.0
CRAP 200.7
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 use v5.20;
10 use strict;
11 use warnings;
12  
13 use Fcntl qw(SEEK_SET);
14 use URI;
15 use URI::file;
16 use Carp;
17 use parent qw(Data::Identifier::Interface::Userdata Data::Identifier::Interface::Subobjects);
18  
19 use constant {
20 CRLF => "\015\012",
21 };
22  
23 our $VERSION = v0.04;
24  
25 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 my ($pkg, $handle, %opts) = @_;
37 my %check = %_check_defaults;
38 my $self = bless {check => \%check}, $pkg;
39  
40 foreach my $key (keys %_check_defaults) {
41 if (exists $opts{$key}) {
42 $check{$key} = delete $opts{$key};
43 }
44 }
45  
46 if (defined(my $baseuri = delete $opts{baseuri})) {
47 $baseuri = URI->new($baseuri) unless eval { $baseuri->isa('URI') };
48 $self->{baseuri} = $baseuri;
49 }
50  
51 croak 'Stray options passed' if scalar keys %opts;
52  
53 unless (ref $handle) {
54 open(my $fh, '<', $handle) or die $!;
55 $handle = $fh;
56 }
57  
58 binmode($handle) or die $!;
59  
60 $self->{fh} = $handle;
61  
62 return $self;
63 }
64  
65  
66 sub write_comment {
67 my ($self, @list) = @_;
68 my $fh = $self->{fh};
69  
70 foreach my $ent (@list) {
71 if (ref($ent) eq 'ARRAY') {
72 $self->write_comment(@{$ent});
73 } elsif (ref($ent)) {
74 print $fh '# ';
75 $self->write_list($ent);
76 } else {
77 foreach my $line (split/\015?\012/, $ent) {
78 print $fh '# ', $line, CRLF;
79 }
80 }
81 }
82 }
83  
84  
85 sub write_list {
86 my ($self, @list) = @_;
87 my $fh = $self->{fh};
88  
89 foreach my $ent (@list) {
90 if (ref($ent) eq 'ARRAY') {
91 $self->write_list(@{$ent});
92 } elsif (ref($ent)) {
93 if ($ent->isa('URI')) {
94 print $fh $ent->as_string, CRLF;
95 } elsif ($ent->isa('Data::Identifier')) {
96 print $fh $ent->uri, CRLF;
97 } elsif ($ent->isa('Data::URIID::Result')) {
98 print $fh $ent->url, CRLF;
99 } elsif ($ent->isa('Data::URIID::Base') || $ent->isa('Data::Identifier::Interface::Simple')) {
100 print $fh $ent->as('uri'), CRLF;
101 } elsif ($ent->isa('Data::Identifier::Cloudlet')) {
102 $self->write_list($ent->roots);
103 } elsif ($ent->isa(__PACKAGE__)) {
104 $ent->read_to(sub {
105 print $fh $_[1]->as_string, CRLF;
106 });
107 } else {
108 croak 'Unsupported object passed';
109 }
110 } else {
111 print $fh URI->new($ent)->as_string, CRLF;
112 }
113 }
114 }
115  
116  
117 sub read_to {
118 my ($self, $cb, %opts) = @_;
119 my $fh = $self->{fh};
120 my $as = delete($opts{as}) // 'URI';
121  
122 croak 'Stray options passed' if scalar keys %opts;
123  
124 # Preload modules as needed:
125 if ($as ne 'URI') {
126 require Data::Identifier;
127 }
128  
129 if (ref($cb) eq 'ARRAY') {
130 my $list = $cb;
131 $cb = sub { push(@{$list}, $_[1]) };
132 }
133  
134 while (defined(my $line = <$fh>)) {
135 $line =~ s/\015?\012$//;
136  
137 next if $line =~ /^#/;
138  
139 if ($line =~ /^\s/ || $line =~ /\s$/) {
140 my $action = $self->{check}{extra_spaces} // '';
141  
142 if ($action eq 'die') {
143 croak 'Input line with extra spaces, aborting';
144 } elsif ($action eq 'trim') {
145 $line =~ s/^\s+//;
146 $line =~ s/\s+$//;
147 } elsif ($action eq 'pass') {
148 # no-op
149 } else {
150 croak 'Input line with extra spaces and bad handling action selected: '.$action;
151 }
152 }
153  
154 if ($line eq '') {
155 my $action = $self->{check}{blank_lines} // '';
156  
157 if ($action eq 'die') {
158 croak 'Blank line in input, aborting';
159 } elsif ($action eq 'skip') {
160 next;
161 } elsif ($action eq 'undef') {
162 $cb->($self, undef);
163 next;
164 } elsif ($action eq 'pass') {
165 # no-op
166 } else {
167 croak 'Blank line in input and bad handling action selected: '.$action;
168 }
169 }
170  
171 if ($line =~ /^\// && $self->{check}{slash_as_local}) {
172 $line = URI::file->new($line);
173 }
174  
175 if (!ref($line) && !defined($self->{baseuri}) && $line !~ /^[a-zA-Z][a-zA-Z0-9\+\.\-]+:/) {
176 my $action = $self->{check}{no_scheme} // '';
177  
178 if ($action eq 'die') {
179 croak 'URI with no scheme, aborting';
180 } elsif ($action eq 'pass') {
181 # no-op
182 } else {
183 croak 'URI with no scheme and bad handling action selected: '.$action;
184 }
185 }
186  
187 unless (ref $line) {
188 if (defined $self->{baseuri}) {
189 $line = URI->new_abs($line, $self->{baseuri});
190 } else {
191 $line = URI->new($line);
192 }
193 }
194  
195 if ($as ne 'URI') {
196 $line = $line->Data::Identifier::as($as);
197 }
198  
199 $cb->($self, $line);
200 }
201 }
202  
203  
204 sub read_as_list {
205 my ($self, %opts) = @_;
206 my $list = [];
207 my $listas = delete $opts{listas};
208  
209 delete $opts{list}; # we are always in list mode
210  
211 $self->read_to($list, %opts);
212  
213 if (defined($listas)) {
214 require Data::Identifier::Cloudlet;
215 $list = Data::Identifier::Cloudlet->new(root => $list)->as($listas);
216 }
217  
218 return $list;
219 }
220  
221  
222 sub rewind {
223 my ($self) = @_;
224 my $fh = $self->{fh};
225  
226 $fh->seek(0, SEEK_SET) or die $!;
227 $fh->input_line_number(1);
228 }
229  
230  
231 sub clear {
232 my ($self) = @_;
233 my $fh = $self->{fh};
234  
235 $self->rewind;
236  
237 $fh->truncate(0);
238 }
239  
240 1;
241  
242 __END__