Astro/Catalog/IO/ASCII.pm

filter: Statementstmt 86.9% 60 / 69 Branchbran 60.0% 18 / 30 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 76.2% 93 / 122 CC 19 SCAR 23.5
CC
19
Cov
77%
read_catalog
14.2
write_catalog
9.6
BEGIN
1.0
CRAP 10.5
« Astro/Catalog/IO/Northstar.pm ↑ Summary Astro/Catalog/IO/Astrom.pm »
1 package Astro::Catalog::IO::ASCII;
2  
3 =head1 NAME
4  
5 Astro::Catalog::IO::ASCII - base class for ASCII-based catalogs
6  
7 =head1 SYNOPSIS
8  
9 $cat = $ioclass->read_catalog(%args);
10  
11 =head1 DESCRIPTION
12  
13 This class provides a wrapper for reading ASCII-based catalogs
14 into C<Astro::Catalog> objects. The method should, in general, only
15 be called from the C<Astro::Catalog> C<configure> method.
16  
17 =cut
18  
19 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 33 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 33 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 33 our $VERSION = '4.38';
25 33 our $DEBUG = 0;
26  
27 =head1 METHODS
28  
29 =over 4
30  
31 =item B<read_catalog>
32  
33 Read the catalog.
34  
35 $cat = $ioclass->read_catalog(%args);
36  
37 Takes a hash as argument with the list of keywords. Supported options
38 are:
39  
40 Data => Contents of catalog, either as a scalar variable,
41 reference to array of lines or reference to glob (file handle).
42 This key is used in preference to 'File' if both are present.
43  
44 File => File name for catalog on disk. Not used if 'Data' supplied.
45 If a file is specified but is called 'default', the default file
46 for the class is used.
47  
48 ReadOpt => Reference to hash of options to be forwarded onto the
49 format specific catalog reader. See the IO documentation
50 for details.
51  
52 The options are case-insensitive.
53  
54 =cut
55  
56 23 ▶sub read_catalog {
Subroutine read_catalog: called CC=12 SCAR=26.6
Pod documented
57 23 my $class = shift;
58  
59 23 my %args = @_;
60 23 %args = Astro::Catalog::_normalize_hash(%args);
61  
62 # Lines for the content
63 23 my @lines;
64  
65 # Now need to either look for some data or read a file
66 23 ▶ if (defined $args{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'data'}) { } 15 8 23
67 # Need to extract the data from this and convert to array
68 15 ▶ if (not ref($args{data})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $args{'data'}) { } 0 15 15
69 # must be a scalar
70 0 @lines = split /\n/, $args{data};
71 }
72 else {
73 15 ▶ if (ref($args{data}) eq 'GLOB' || UNIVERSAL::isa($args{data},"IO::Handle")) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args{'data'} eq 'GLOB' or UNIVERSAL::isa($args{'data'}, 'IO::Handle')) { } 13 2 15
elsif (ref $args{'data'} eq 'ARRAY') { } 1 1 2
Condition100%3/3
ref $args{"data"} eq "GLOB" or UNIVERSAL::isa($args{"data"}, "IO::Handle")
l!l&&r!l&&!r
1212
MC/DC100%2/2
ref $args{"data"} eq "GLOB" or UNIVERSAL::isa($args{"data"}, "IO::Handle")
ref $args{"data"} eq "GLOB" UNIVERSAL::isa($args{"data"}, "IO::Handle")
Truth table100%3/3
A or B
A= ref $args{"data"} eq "GLOB"B= UNIVERSAL::isa($args{"data"}, "IO::Handle")
ABresult
1X1
011
000
74 # A file handle
75 13 local $/ = "\n";
76 # For some reason <$args{data}> does not do the right thing
77 13 my $fh = $args{data};
78 13 @lines = <$fh>;
79 }
80 elsif (ref($args{data}) eq 'ARRAY') {
81 # An array of lines
82 1 @lines = @{ $args{data} };
83 }
84 else {
85 # Who knows
86 1 croak "Can not extract catalog information from scalar of type " . ref($args{data}) ."\n";
87 }
88 }
89 }
90 else {
91 # Look for a filename or the default file
92 8 my $file;
93 8 ▶ if (defined $args{file} && $args{file} ne 'default') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'file'} and $args{'file'} ne 'default') { } 8 0 8
Condition33%1/3
defined $args{"file"} and $args{"file"} ne "default"
!ll&&!rl&&r
008
MC/DC0%0/2
defined $args{"file"} and $args{"file"} ne "default"
defined $args{"file"} $args{"file"} ne "default"
Truth table33%1/3
A and B
A= defined $args{"file"}B= $args{"file"} ne "default"
ABresult
0X0
100
111
94 8 $file = $args{file};
95 }
96 else {
97 # Need to ask for the default file
98 0 ▶ $file = $class->_default_file() if $class->can('_default_file');
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("_default_file") 0 0 0
99 0 ▶ croak "Unable to read catalog since no file specified and no default known."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
100 unless defined $file;
101 }
102  
103 # Open the file
104 8 my $CAT;
105 8 ▶ croak("Astro::Catalog - Cannot open catalog file $file: $!")
Branch: 1 branch
BranchTrueFalseTotal
unless open $CAT, "< $file" 0 8 8
106 unless open($CAT, "< $file");
107  
108 # read from file
109 8 local $/ = "\n";
110 8 @lines = <$CAT>;
111 8 close($CAT);
112 }
113  
114 # remove new lines
115 22 chomp @lines;
116  
117 # Read Catalog options passed in from caller
118 22 ▶ my $readopt = (defined $args{readopt} ? $args{readopt} : {});
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'readopt'} ? : 0 22 22
119  
120 22 my $catalog = $class->_read_catalog(\@lines, %$readopt);
121  
122 22 return $catalog;
123 }
124  
125 =item B<write_catalog>
126  
127 Write the catalog.
128  
129 $ioclass->write_catalog($catalog, %args);
130  
131 Takes a hash as argument with the list of keywords. Supported options
132 are:
133  
134 File => File name for catalog on disk.
135  
136 The options are case-insensitive. Other options are forwarded
137 to the format-specific catalog writer.
138  
139 =cut
140  
141 11 ▶sub write_catalog {
Subroutine write_catalog: called CC=8 SCAR=22.6
Pod documented
142 11 my $class = shift;
143 11 my $catalog = shift;
144  
145 11 my %args = @_;
146 11 %args = Astro::Catalog::_normalize_hash(%args);
147  
148 11 my $file = $args{file};
149 11 delete $args{file};
150  
151 11 my $lines = $class->_write_catalog($catalog, %args);
152  
153 # Play it defensively - make sure we add the newlines
154 11 chomp @$lines;
155  
156 # If we have a reference then we do not need to open or close
157 # files - simpler to deal with each case in turn. This has the
158 # side effect of repeating the join() in 3 separate places.
159 # Probably better than creating a large scalar for the one time
160 # when we do not need it.
161  
162 11 my $retval = 1;
163 11 ▶ if (ref($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 6 5 11
164 # If we are storing in a reference to a scalar or reference
165 # to an array, just do the copy and return early. We do not
166 6 ▶ if (ref($file) eq 'SCALAR') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $file eq 'SCALAR') { } 0 6 6
elsif (ref $file eq 'ARRAY') { } 1 5 6
elsif (ref $file eq 'GLOB' or $file->can('print')) { } 5 0 5
167 # Copy single string to scalar
168 0 $$file = join("\n", @$lines) ."\n";
169 }
170 elsif (ref($file) eq 'ARRAY') {
171 # Just copy the lines into the output array
172 1 @$file = @$lines;
173 }
174 ▶ elsif (ref($file) eq 'GLOB' || $file->can("print") ) {
Condition33%1/3
ref $file eq "GLOB" or $file->can("print")
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $file eq "GLOB" or $file->can("print")
ref $file eq "GLOB" $file->can("print")
Truth table33%1/3
A or B
A= ref $file eq "GLOB"B= $file->can("print")
ABresult
1X1
011
000
175 # GLOB - so print the full string to the file handle and flush
176 5 $retval = print $file join("\n", @$lines) ."\n";
177 5 autoflush $file 1; # We need to make sure we write the lines
178 }
179 else {
180 0 croak "Can not write catalog to reference of type ".
181 ref($file)."\n";
182 }
183 }
184 else {
185 # A file name
186 5 my $status = open my $fh, ">$file";
187 5 ▶ unless ($status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status) 0 5 5
188 0 $catalog->errstr(__PACKAGE__ .": Error creating catalog file $file: $!" );
189 0 return;
190 }
191  
192 # write to file
193 5 $retval = print $fh join("\n", @$lines) ."\n";
194  
195 # close file
196 5 $status = close($fh);
197 5 ▶ unless ($status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status) 0 5 5
198 0 $catalog->errstr(__PACKAGE__.": Error closing catalog file $file: $!");
199 0 return;
200 }
201 }
202  
203 # everything okay
204 11 return $retval;
205 }
206  
207 33 1;
208  
209 __END__
« Astro/Catalog/IO/Northstar.pm ↑ Summary Astro/Catalog/IO/Astrom.pm »