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