blib/lib/Chemistry/File/Dumper.pm

filter: Statementstmt 90.4% 38 / 42 Branchbran 50.0% 6 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.0% 9 / 10 Podpod 100% 4 / 4 totaltotal 83.8% 57 / 68 CC 7 SCAR 10.1
CC
7
Cov
81%
write_mol
4.4
read_mol
4.3
string_is
2.0
CRAP 2.8
« blib/lib/Chemistry/InternalCoords.pm ↑ Summary blib/lib/Chemistry/Bond.pm »
1 package Chemistry::File::Dumper;
2 20 $VERSION = '0.35';
3  
4 20 require 5.006;
5 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use base "Chemistry::File";
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use Chemistry::Mol; # should I use it?
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 purpuses 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<all> 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 20 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 6 ▶sub write_mol {
Subroutine write_mol: called CC=4 SCAR=14.9
Pod documented
68 6 my ($self, $fh, $mol, %opts) = @_;
69 6 my $d = Data::Dumper->new([$mol],['$mol']);
70 # sort the keys if this version of Data::Dumper supports it
71 6 ▶ $d->Sortkeys(1) if $d->can('Sortkeys');
Branch: 1 branch
BranchTrueFalseTotal
if $d->can("Sortkeys") 6 0 6
72 print $fh $d
73 ->Indent(exists $opts{dumper_indent} ? $opts{dumper_indent} : 1)
74 6 ▶ ->Purity(exists $opts{dumper_purity} ? $opts{dumper_purity} : 1)
Branch: 2 branches
BranchTrueFalseTotal
exists $opts{'dumper_indent'} ? : 0 6 6
exists $opts{'dumper_purity'} ? : 0 6 6
75 ->Dump;
76 }
77  
78 30 ▶sub read_mol {
Subroutine read_mol: called CC=4 SCAR=14.7
Pod documented
79 30 my ($self, $fh, %opts) = @_;
80 30 my $mol;
81 30 my $s = do { local $/; <$fh> };
82 30 ▶ return unless $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 15 15 30
83 15 eval $s;
84 15 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 15 15
85 0 ▶ croak "Dumper eval error: $@" if $opts{fatal};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"fatal"} 0 0 0
86 0 return;
87 }
88 15 $mol->_weaken;
89 15 $mol;
90 }
91  
92 12 ▶sub name_is {
Subroutine name_is: called CC=1 SCAR=0.0
Pod documented
93 12 my ($self, $name, %opts) = @_;
94 12 $name =~ /\.pl$/i;
95 }
96  
97 0 ▶sub string_is {
Subroutine string_is: not called CC=1 SCAR=6.9
Pod documented
98 0 my ($self, $s, %opts) = @_;
99 0 $s =~ /^\$mol/;
100 }
101  
102 20 1;
103  
104 =head1 VERSION
105  
106 0.35
107  
108 =head1 SEE ALSO
109  
110 L<Chemistry::Mol>, L<Chemistry::File>, L<Data::Dumper>
111  
112 =head1 AUTHOR
113  
114 Ivan Tubert-Brohman <itub@cpan.org>
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  
« blib/lib/Chemistry/InternalCoords.pm ↑ Summary blib/lib/Chemistry/Bond.pm »