Astro/Catalog/IO/Binary.pm

filter: Statementstmt 25.4% 15 / 59 Branchbran 0.0% 0 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 4 / 6 Podpod 100% 2 / 2 totaltotal 21.2% 21 / 99 CC 17 SCAR 54.7
CC
17
Cov
16%
read_catalog
306.0
write_catalog
2.0
BEGIN
1.0
CRAP 236.7
« Astro/Catalog/IO/ASSM.pm ↑ Summary Astro/Catalog/IO/Hedwig.pm »
1 package Astro::Catalog::IO::Binary;
2  
3 =head1 NAME
4  
5 Astro::Catalog::IO::Binary - base class for binary 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 binary 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 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 our $VERSION = '4.38';
25 1 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, as a reference to glob (file handle)
41 or a scalar containing data to be turned into a catalog.
42 This key is used in preference to 'File' if both are present.
43 File => File name for catalog on disk. Not used if 'Data' supplied.
44 ReadOpt => Reference to hash of options to be forwarded onto the
45 format specific catalog reader. See the IO documentation
46 for details.
47  
48 The options are case-insensitive.
49  
50 =cut
51  
52 0 ▶sub read_catalog {
Subroutine read_catalog: not called CC=17 SCAR=57.2
Pod documented
53 0 my $class = shift;
54  
55 0 my $catalog;
56  
57 # Retrieve and normalize arguments.
58 0 my %args = @_;
59 0 %args = Astro::Catalog::_normalize_hash(%args);
60  
61 0 ▶ my $readopt = (defined $args{readopt} ? $args{readopt} : {});
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'readopt'} ? : 0 0 0
62  
63 # Find out if the class would rather have a file handle or
64 # a file name.
65 0 my $input_format = $class->input_format;
66  
67 # Now need to either look for some data or read a file
68 0 ▶ if (defined $args{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'data'}) { } 0 0 0
69 0 ▶ if (ref($args{data}) eq 'GLOB') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args{'data'} eq 'GLOB') { } 0 0 0
elsif (not ref $args{'data'}) { } 0 0 0
70 # A file handle. If the requested input format is a file handle,
71 # then we're good. If the requested input format is a file name,
72 # then copy the file pointed to by the file handle to a temporary
73 # file, then pass that file name to the IO class.
74 0 ▶ if ($input_format eq 'handle') {
Branch: 2 branches
BranchTrueFalseTotal
if ($input_format eq 'handle') { } 0 0 0
elsif ($input_format eq 'name') { } 0 0 0
75 $catalog = $class->_read_catalog(
76 filehandle => $args{data},
77 0 %$readopt);
78 }
79 elsif ($input_format eq 'name') {
80 0 (my $fh, my $filename) = tempfile(UNLINK => 1);
81 0 binmode $args{data};
82 0 ▶ while (read $args{data}, my $buffer, 1024) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $args{"data"}, my $buffer, 1024) 0 0 0
83 0 print $fh $buffer;
84 }
85 0 close $fh;
86 0 $catalog = $class->_read_catalog(
87 filename => $filename,
88 %$readopt);
89 }
90 else {
91 # We got back something we can't use.
92 0 croak "Unknown input format $input_format";
93 }
94 }
95 elsif (not ref($args{data})) {
96 0 (my $fh, my $filename) = tempfile(UNLINK => 1);
97 0 print $fh $args{data};
98 0 close $fh;
99  
100 0 ▶ if ($input_format eq 'handle') {
Branch: 2 branches
BranchTrueFalseTotal
if ($input_format eq 'handle') { } 0 0 0
elsif ($input_format eq 'name') { } 0 0 0
101 0 ▶ open($fh, $filename) or croak "Could not open file $filename for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $filename or croak("Could not open file $filename for reading: $!") 0 0 0
102 0 $catalog = $class->_read_catalog(
103 filehandle => $fh,
104 %$readopt);
105 0 close $fh;
106 }
107 elsif ($input_format eq 'name') {
108 0 $catalog = $class->_read_catalog(
109 filename => $filename,
110 %$readopt);
111 }
112 }
113 else {
114 # Who knows
115 0 croak "Can not extract catalog information from scalar of type " . ref($args{data}) ."\n";
116 }
117 }
118 else {
119 # Look for a filename or the default file
120 0 my $file;
121 0 ▶ if (defined $args{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'file'}) { } 0 0 0
122 0 $file = $args{file};
123 }
124 else {
125 # Need to ask for the default file
126 0 ▶ $file = $class->_default_file() if $class->can('_default_file');
Branch: 1 branch
BranchTrueFalseTotal
if $class->can("_default_file") 0 0 0
127 0 ▶ croak "Unable to read catalog since no file specified and no default known."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
128 unless defined $file;
129 }
130  
131 # Pass along the desired input format.
132 0 ▶ if ($input_format eq 'handle') {
Branch: 2 branches
BranchTrueFalseTotal
if ($input_format eq 'handle') { } 0 0 0
elsif ($input_format eq 'name') { } 0 0 0
133 0 ▶ open(my $fh, $file) or croak "Could not open file $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $file or croak("Could not open file ${file}: $!") 0 0 0
134 0 $catalog = $class->_read_catalog(
135 filehandle => $fh,
136 %$readopt );
137 }
138 elsif ($input_format eq 'name') {
139 0 $catalog = $class->_read_catalog(
140 filename => $file,
141 %$readopt );
142 }
143 else {
144 0 croak "Unknown input format $input_format";
145 }
146 }
147  
148 0 return $catalog;
149 }
150  
151 =item B<write_catalog>
152  
153 Write the catalog.
154  
155 $ioclass->write_catalog($catalog, %args);
156  
157 Takes a hash as argument with the list of keywords. Supported options
158 are:
159  
160 File => File name for catalog on disk.
161  
162 The options are case-insensitive. Other options are forwarded
163 to the format-specific catalog writer.
164  
165 =cut
166  
167 0 ▶sub write_catalog {
Subroutine write_catalog: not called CC=1 SCAR=6.9
Pod documented
168 0 my $class = shift;
169 0 my $catalog = shift;
170  
171 0 my %args = @_;
172 0 %args = Astro::Catalog::_normalize_hash(%args);
173  
174 0 my $file = $args{file};
175 0 delete $args{file};
176  
177 0 croak "Not yet implemented.";
178 }
179  
180 1 1;
181  
182 __END__
« Astro/Catalog/IO/ASSM.pm ↑ Summary Astro/Catalog/IO/Hedwig.pm »