File Coverage

blib/lib/Chemistry/File/Dumper.pm
Criterion Covered Total %
statement 34 38 89.4
branch 6 12 50.0
condition n/a
subroutine 9 10 90.0
pod 4 4 100.0
total 53 64 82.8


line stmt bran cond sub pod time code
1             package Chemistry::File::Dumper;
2             $VERSION = '0.37';
3              
4             require 5.006;
5 11     11   255185 use strict;
  11         31  
  11         416  
6 11     11   63 use warnings;
  11         20  
  11         793  
7 11     11   112 use base "Chemistry::File";
  11         20  
  11         13215  
8 11     11   10409 use Chemistry::Mol; # should I use it?
  11         43  
  11         755  
9 11     11   17963 use Data::Dumper;
  11         113505  
  11         1298  
10 11     11   118 use Carp;
  11         24  
  11         5032  
11              
12             =head1 NAME
13              
14             Chemistry::File::Dumper - Read and write molecules via Data::Dumper
15              
16             =head1 SYNOPSIS
17              
18             use Chemistry::File::Dumper;
19              
20             my $mol = Chemistry::Mol->read("mol.pl");
21             print $mol->print(format => dumper);
22             $mol->write("mol.pl", format => "dumper");
23              
24             =cut
25              
26              
27              
28             =head1 DESCRIPTION
29              
30             This module hooks the Data::Dumper Perl core module to the Chemistry::File
31             API, allowing you to dump and undump Chemistry::Mol objects easily.
32             This module automatically registers the "dumper" format with Chemistry::Mol.
33              
34             For purposes of automatic file type guessing, this module assumes that
35             dumped files end in C<.pl>.
36              
37             This module is useful mainly for debugging purposes, as it dumps I the
38             information available in an object, in a reproducible way (so you can use
39             it to compare molecule objects). However, it wouldn't be a good idea to use it
40             to read untrusted files, because they may contain arbitrary Perl code.
41              
42             =cut
43              
44             Chemistry::Mol->register_format(dumper => __PACKAGE__);
45              
46             =head1 OPTIONS
47              
48             The following options can be used when writing a molecule either as
49             a file or as a string.
50              
51             =over 4
52              
53             =item dumper_indent
54              
55             Value to give to Data::Dumper::Indent. Default is 1.
56              
57             =item dumper_purity
58              
59             Value to give to Data::Dumper::Purity. Default is 1.
60              
61             =back
62              
63             There are no special options for reading.
64              
65             =cut
66              
67             sub write_mol {
68 6     6 1 26 my ($self, $fh, $mol, %opts) = @_;
69 6         67 my $d = Data::Dumper->new([$mol],['$mol']);
70             # sort the keys if this version of Data::Dumper supports it
71 6 50       277 $d->Sortkeys(1) if $d->can('Sortkeys');
72 6 50       77 print $fh $d
    50          
73             ->Indent(exists $opts{dumper_indent} ? $opts{dumper_indent} : 1)
74             ->Purity(exists $opts{dumper_purity} ? $opts{dumper_purity} : 1)
75             ->Dump;
76             }
77              
78             sub read_mol {
79 28     28 1 103 my ($self, $fh, %opts) = @_;
80 28         46 my $mol;
81 28         43 my $s = do { local $/; <$fh> };
  28         127  
  28         1245  
82 28 100       161 return unless $s;
83 14         55844 eval $s;
84 14 50       202 if ($@) {
85 0 0       0 croak "Dumper eval error: $@" if $opts{fatal};
86 0         0 return;
87             }
88 14         107 $mol->_weaken;
89 14         124 $mol;
90             }
91              
92             sub name_is {
93 10     10 1 33 my ($self, $name, %opts) = @_;
94 10         130 $name =~ /\.pl$/i;
95             }
96              
97             sub string_is {
98 0     0 1   my ($self, $s, %opts) = @_;
99 0           $s =~ /^\$mol/;
100             }
101              
102             1;
103              
104             =head1 VERSION
105              
106             0.37
107              
108             =head1 SEE ALSO
109              
110             L, L, L
111              
112             =head1 AUTHOR
113              
114             Ivan Tubert-Brohman
115              
116             =head1 COPYRIGHT
117              
118             Copyright (c) 2005 Ivan Tubert-Brohman. All rights reserved. This program is
119             free software; you can redistribute it and/or modify it under the same terms as
120             Perl itself.
121              
122             =cut
123