File Coverage

blib/lib/Astro/Catalog/IO/Binary.pm
Criterion Covered Total %
statement 18 55 32.7
branch 0 30 0.0
condition n/a
subroutine 6 7 85.7
pod 1 1 100.0
total 25 93 26.8


line stmt bran cond sub pod time code
1             package Astro::Catalog::IO::Binary;
2              
3             =head1 NAME
4              
5             Astro::Catalog::IO::Binary - base class for binary catalogues.
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 catalogues
14             into C objects. The method should, in general, only
15             be called from the C C method.
16              
17             =cut
18              
19 1     1   2718736 use 5.006;
  1         11  
  1         75  
20 1     1   7 use warnings;
  1         2  
  1         106  
21 1     1   68 use warnings::register;
  1         3  
  1         378  
22 1     1   7 use Carp;
  1         9  
  1         223  
23 1     1   5 use strict;
  1         2  
  1         111  
24              
25 1     1   5 use vars qw/ $VERSION $DEBUG /;
  1         2  
  1         780  
26              
27             $VERSION = '4.31';
28             $DEBUG = 0;
29              
30             =head1 METHODS
31              
32             =over 4
33              
34             =item B
35              
36             Read the catalog.
37              
38             $cat = $ioclass->read_catalog( %args );
39              
40             Takes a hash as argument with the list of keywords. Supported options
41             are:
42              
43             Data => Contents of catalogue, as a reference to glob (file handle)
44             or a scalar containing data to be turned into a catalog.
45             This key is used in preference to 'File' if both are present.
46             File => File name for catalog on disk. Not used if 'Data' supplied.
47             ReadOpt => Reference to hash of options to be forwarded onto the
48             format specific catalogue reader. See the IO documentation
49             for details.
50              
51             The options are case-insensitive.
52              
53             =cut
54              
55             sub read_catalog {
56 0     0 1   my $class = shift;
57              
58 0           my $catalog;
59              
60             # Retrieve and normalize arguments.
61 0           my %args = @_;
62 0           %args = Astro::Catalog::_normalize_hash( %args );
63              
64 0 0         my $readopt = (defined $args{readopt} ? $args{readopt} : {} );
65              
66             # Find out if the class would rather have a file handle or
67             # a file name.
68 0           my $input_format = $class->input_format;
69              
70             # Now need to either look for some data or read a file
71 0 0         if ( defined $args{data}) {
72              
73 0 0         if (ref($args{data}) eq 'GLOB') {
    0          
74             # A file handle. If the requested input format is a file handle,
75             # then we're good. If the requested input format is a file name,
76             # then copy the file pointed to by the file handle to a temporary
77             # file, then pass that file name to the IO class.
78 0 0         if( $input_format eq 'handle' ) {
    0          
79              
80 0           $catalog = $class->_read_catalog( filehandle => $args{data},
81             %$readopt );
82              
83             } elsif( $input_format eq 'name' ) {
84              
85 0           ( my $fh, my $filename ) = tempfile( UNLINK => 1 );
86 0           binmode $args{data};
87 0           while( read $args{data}, my $buffer, 1024 ) {
88 0           print $fh $buffer;
89             }
90 0           close $fh;
91 0           $catalog = $class->_read_catalog( filename => $filename,
92             %$readopt );
93             } else {
94              
95             # We got back something we can't use.
96 0           croak "Unknown input format $input_format";
97              
98             }
99             } elsif( not ref( $args{data} ) ) {
100              
101 0           ( my $fh, my $filename ) = tempfile( UNLINK => 1 );
102 0           print $fh $args{data};
103 0           close $fh;
104              
105 0 0         if( $input_format eq 'handle' ) {
    0          
106 0 0         open( $fh, $filename ) or croak "Could not open file $filename for reading: $!";
107 0           $catalog = $class->_read_catalog( filehandle => $fh,
108             %$readopt );
109 0           close $fh;
110             } elsif( $input_format eq 'name' ) {
111              
112 0           $catalog = $class->_read_catalog( filename => $filename,
113             %$readopt );
114             }
115             } else {
116             # Who knows
117 0           croak "Can not extract catalog information from scalar of type " . ref($args{data}) ."\n";
118             }
119              
120             } else {
121             # Look for a filename or the default file
122 0           my $file;
123 0 0         if ( defined $args{file} ) {
124 0           $file = $args{file};
125             } else {
126             # Need to ask for the default file
127 0 0         $file = $class->_default_file() if $class->can( '_default_file' );
128 0 0         croak "Unable to read catalogue since no file specified and no default known." unless defined $file;
129             }
130              
131             # Pass along the desired input format.
132 0 0         if( $input_format eq 'handle' ) {
    0          
133              
134 0 0         open( my $fh, $file ) or croak "Could not open file $file: $!";
135 0           $catalog = $class->_read_catalog( filehandle => $fh,
136             %$readopt );
137              
138             } elsif( $input_format eq 'name' ) {
139              
140 0           $catalog = $class->_read_catalog( filename => $file,
141             %$readopt );
142              
143             } else {
144              
145 0           croak "Unknown input format $input_format";
146             }
147              
148             }
149              
150 0           return $catalog;
151             }
152              
153             =back
154              
155             =head1 REVISION
156              
157             $Id: Binary.pm,v 1.1 2005/03/31 01:26:07 cavanagh Exp $
158              
159             =head1 SEE ALSO
160              
161             L
162              
163             =head1 COPYRIGHT
164              
165             Copyright (C) 2005 Particle Physics and Astronomy Research Council.
166             All Rights Reserved.
167              
168             This program is free software; you can redistribute it and/or modify it under
169             the terms of the GNU General Public License as published by the Free Software
170             Foundation; either version 2 of the License, or (at your option) any later
171             version.
172              
173             This program is distributed in the hope that it will be useful,but WITHOUT ANY
174             WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
175             PARTICULAR PURPOSE. See the GNU General Public License for more details.
176              
177             You should have received a copy of the GNU General Public License along with
178             this program; if not, write to the Free Software Foundation, Inc., 59 Temple
179             Place,Suite 330, Boston, MA 02111-1307, USA
180              
181             =head1 AUTHORS
182              
183             Brad Cavanagh Eb.cavanagh@jach.hawaii.eduE
184              
185             =cut
186              
187             1;