| 1 | package Chemistry::File::Dumper; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 11 | our $VERSION = '0.40'; # VERSION | |||||||||||||
| 4 | |||||||||||||||
| 5 | 11 | require 5.006; | |||||||||||||
| 6 | 11 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 11 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 11 | ▶ | use base "Chemistry::File"; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 11 | ▶ | use Chemistry::Mol; # should I use it? | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 11 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 11 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 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<all> 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 | 11 | 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 | 6 | ▶ | sub write_mol { | ||||||||||||
|
Subroutine
write_mol: called CC=4 SCAR=14.9
Pod
documented
| |||||||||||||||
| 69 | 6 | my ($self, $fh, $mol, %opts) = @_; | |||||||||||||
| 70 | 6 | my $d = Data::Dumper->new([$mol],['$mol']); | |||||||||||||
| 71 | # sort the keys if this version of Data::Dumper supports it | ||||||||||||||
| 72 | 6 | ▶ | $d->Sortkeys(1) if $d->can('Sortkeys'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 73 | print $fh $d | ||||||||||||||
| 74 | ->Indent(exists $opts{dumper_indent} ? $opts{dumper_indent} : 1) | ||||||||||||||
| 75 | 6 | ▶ | ->Purity(exists $opts{dumper_purity} ? $opts{dumper_purity} : 1) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 76 | ->Dump; | ||||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 30 | ▶ | sub read_mol { | ||||||||||||
|
Subroutine
read_mol: called CC=4 SCAR=14.7
Pod
documented
| |||||||||||||||
| 80 | 30 | my ($self, $fh, %opts) = @_; | |||||||||||||
| 81 | 30 | my $mol; | |||||||||||||
| 82 | 30 | my $s = do { local $/; <$fh> }; | |||||||||||||
| 83 | 30 | ▶ | return unless $s; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | 15 | eval $s; | |||||||||||||
| 85 | 15 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | 0 | ▶ | croak "Dumper eval error: $@" if $opts{fatal}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | 0 | return; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | 15 | $mol->_weaken; | |||||||||||||
| 90 | 15 | $mol; | |||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | 13 | ▶ | sub name_is { | ||||||||||||
|
Subroutine
name_is: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 94 | 13 | my ($self, $name, %opts) = @_; | |||||||||||||
| 95 | 13 | $name =~ /\.pl$/i; | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | 0 | ▶ | sub string_is { | ||||||||||||
|
Subroutine
string_is: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 99 | 0 | my ($self, $s, %opts) = @_; | |||||||||||||
| 100 | 0 | $s =~ /^\$mol/; | |||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | 11 | 1; | |||||||||||||
| 104 | |||||||||||||||
| 105 | =head1 SOURCE CODE REPOSITORY | ||||||||||||||
| 106 | |||||||||||||||
| 107 | L<https://github.com/perlmol/Chemistry-Mol> | ||||||||||||||
| 108 | |||||||||||||||
| 109 | =head1 SEE ALSO | ||||||||||||||
| 110 | |||||||||||||||
| 111 | L<Chemistry::Mol>, L<Chemistry::File>, L<Data::Dumper> | ||||||||||||||
| 112 | |||||||||||||||
| 113 | =head1 AUTHOR | ||||||||||||||
| 114 | |||||||||||||||
| 115 | Ivan Tubert-Brohman <itub@cpan.org> | ||||||||||||||
| 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 | |||||||||||||||