Obj.pm

filter: Statementstmt 81.5% 62 / 76 Branchbran 77.7% 14 / 18 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 84.2% 16 / 19 Podpod 33.3% 4 / 12 totaltotal 71.8% 97 / 135 CC 15 SCAR 14.2
CC
15
Cov
77%
print_attr
12.0
use
12.0
attr
5.0
CRAP 4.1
« File/Formula.pm ↑ Summary File/Dumper.pm »
1 package Chemistry::Obj;
2  
3 17 our $VERSION = '0.40'; # VERSION
4 # $Id$
5  
6 17 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Chemistry::Obj - Abstract chemistry object
14  
15 =head1 SYNOPSIS
16  
17 package MyObj;
18 use base "Chemistry::Obj";
19 Chemistry::Obj::accessor('color', 'flavor');
20  
21 package main;
22 my $obj = MyObj->new(name => 'bob', color => 'red');
23 $obj->attr(size => 42);
24 $obj->color('blue');
25 my $color = $obj->color;
26 my $size = $obj->attr('size');
27  
28 =head1 DESCRIPTION
29  
30 This module implements some generic methods that are used by L<Chemistry::Mol>,
31 L<Chemistry::Atom>, L<Chemistry::Bond>, L<Chemistry::File>, etc.
32  
33 =head2 Common Attributes
34  
35 There are some common attributes that may be found in molecules, bonds, and
36 atoms, such as id, name, and type. They are all accessed through the methods of
37 the same name. For example, to get the id, call C<< $obj->id >>; to set the id,
38 call C<< $obj->id('new_id') >>.
39  
40 =over 4
41  
42 =item id
43  
44 Objects should have a unique ID. The user has the responsibility for uniqueness
45 if he assigns ids; otherwise a unique ID is assigned sequentially.
46  
47 =item name
48  
49 An arbitrary name for an object. The name doesn't need to be unique.
50  
51 =item type
52  
53 The interpretation of this attribute is not specified here, but it's typically
54 used for bond orders and atom types.
55  
56 =item attr
57  
58 A space where the user can store any kind of information about the object. The
59 accessor method for attr expects the attribute name as the first parameter, and
60 (optionally) the new value as the second parameter. It can also take a hash or
61 hashref with several attributes. Examples:
62  
63 $color = $obj->attr('color');
64 $obj->attr(color => 'red');
65 $obj->attr(color => 'red', flavor => 'cherry');
66 $obj->attr({color => 'red', flavor => 'cherry'});
67  
68 =cut
69  
70 16 ▶sub attr {
Subroutine attr: called CC=5 SCAR=16.1
Pod documented
71 16 my $self = shift;
72 16 my ($attr) = @_;
73 16 ▶ if (ref $attr eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $attr eq 'HASH') { } 1 15 16
elsif (@_ == 1) { } 7 8 15
elsif (@_ == 0) { } 2 6 8
74 1 $self->{attr} = { %$attr };
75 } elsif (@_ == 1) {
76 7 return $self->{attr}{$attr};
77 } elsif (@_ == 0) {
78 2 return {%{$self->{attr}}};
79 } else {
80 6 ▶ while (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 7 6 13
81 7 $attr = shift;
82 7 $self->{attr}{$attr} = shift;
83 }
84 }
85 7 $self;
86 }
87  
88 =back
89  
90 =head1 OTHER METHODS
91  
92 =over
93  
94 =item $obj->del_attr($attr_name)
95  
96 Delete an attribute.
97  
98 =cut
99  
100 1 ▶sub del_attr {
Subroutine del_attr: called CC=1 SCAR=0.0
Pod documented
101 1 my $self = shift;
102 1 my $attr = shift;
103 1 delete $self->{attr}{$attr};
104 }
105  
106 # A generic class attribute set/get method generator
107 127 ▶sub accessor {
Subroutine accessor: called CC=2 SCAR=6.9
Pod undocumented
108 127 my $pkg = caller;
109 17 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
110 127 for my $attribute (@_) {
111 199 *{"${pkg}::$attribute"} =
112 989 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
113 989 my $self = shift;
114 989 ▶ return $self->{$attribute} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 815 174 989
115 174 $self->{$attribute} = shift;
116 174 return $self;
117 199 };
118 }
119 }
120  
121 0 ▶sub print_attr {
Subroutine print_attr: not called CC=3 SCAR=24.8
Pod undocumented
122 0 my $self = shift;
123 0 my ($indent) = @_;
124 0 my $ret = '';
125
126 0 for my $attr (keys %{$self->{attr}}) {
127 0 $ret .= "$attr: ".$self->attr($attr)."\n";
128 }
129 0 ▶ $ret and $ret =~ s/^/" "x$indent/gem;
Branch: 1 branch
BranchTrueFalseTotal
$ret and $ret =~ s/^/" " x $indent;/egm 0 0 0
130 0 $ret;
131 }
132  
133 17 my $N = 0; # atom ID counter
134 28 ▶sub nextID { "obj".++$N; }
Subroutine nextID: called CC=1 SCAR=0.0
Pod undocumented
135 0 ▶sub reset_id { $N = 0; }
Subroutine reset_id: not called CC=1 SCAR=6.9
Pod undocumented
136  
137 =item $class->new(name => value, name => value...)
138  
139 Generic object constructor. It will automatically call each "name" method with
140 the parameter "value". For example,
141  
142 $bob = Chemistry::Obj->new(name => 'bob', attr => {size => 42});
143  
144 is equivalent to
145  
146 $bob = Chemistry::Obj->new;
147 $bob->name('bob');
148 $bob->attr({size => 42});
149  
150 =cut
151  
152 28 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
153 28 my $class = shift;
154 28 my %args = @_;
155 28 ▶ my $self = bless {
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0280
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
156 id => $class->nextID,
157 #$class->default_args,
158 }, ref $class || $class;
159 28 $self->$_($args{$_}) for (keys %args);
160 28 return $self;
161 }
162  
163 #sub default_args { (id => shift->nextID) }
164  
165 =back
166  
167 =head1 OPERATOR OVERLOADING
168  
169 Chemistry::Obj overloads a couple of operators for convenience.
170  
171 =over
172  
173 =cut
174  
175 use overload
176 17 ▶ '""' => "stringify",
Subroutine BEGIN: called CC=1 SCAR=0.0
177 'cmp' => "obj_cmp",
178 '0+', => "as_number",
179 fallback => 1,
180 17 ;
181  
182 =item ""
183  
184 The stringification operator. Stringify an object as its id. For example, If an
185 object $obj has the id 'a1', print "$obj" will print 'a1' instead of something
186 like 'Chemistry::Obj=HASH(0x810bbdc)'. If you really want to get the latter,
187 you can call C<overload::StrVal($obj)>. See L<overload> for details.
188  
189 =cut
190  
191 11 ▶sub stringify {
Subroutine stringify: called CC=1 SCAR=0.0
Pod undocumented
192 11 my $self = shift;
193 11 $self->id;
194 }
195  
196 14 ▶sub as_number {
Subroutine as_number: called CC=1 SCAR=0.0
Pod undocumented
197 14 $_[0];
198 }
199  
200 =item cmp
201  
202 Compare objects by ID. This automatically overloads C<eq>, C<ne>, C<lt>, C<le>,
203 C<gt>, and C<ge> as well. For example, C<$obj1 eq $obj2> returns true if both
204 objects have the same id, even if they are different objects with different
205 memory addresses. In contrast, C<$obj1 == $obj2> will return true only if
206 C<$obj1> and C<$obj2> point to the same object, with the same memory address.
207  
208 =cut
209  
210 151 ▶sub obj_cmp {
Subroutine obj_cmp: called CC=1 SCAR=0.0
Pod undocumented
211 151 my ($a, $b) = @_;
212 17 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
213  
214 151 return $a->{id} cmp $b->{id};
215 }
216  
217 =back
218  
219 =cut
220  
221 17 accessor(qw(name type));
222  
223 663 ▶sub id {
Subroutine id: called CC=3 SCAR=11.0
Pod documented
224 663 my $self = shift;
225 663 ▶ return $self->{id} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 654 9 663
226 9 ▶ if ($self->{parent}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'parent'}) { } 4 5 9
227 4 my $new_id = shift;
228 4 my $old_id = $self->{id};
229 4 $self->{id} = $new_id;
230 4 $self->{parent}->_change_id($old_id, $new_id);
231 } else {
232 5 $self->{id} = shift;
233 }
234 }
235  
236 # this is an experimental method and shouldn't be used!
237 0 ▶sub use {
Subroutine use: not called CC=3 SCAR=24.8
Pod undocumented
238 0 my ($pack, $module, @args) = @_;
239 0 ▶ $pack = ref $pack || $pack;
Condition0%0/3
ref $pack || $pack
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $pack || $pack
ref $pack $pack
Truth table0%0/3
A || B
A= ref $packB= $pack
ABresult
1X1
011
000
240 0 ▶ my $args = @args ? "(@args)" : '';
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 0 0 0
241 0 eval "package $pack; use $module $args";
242 }
243  
244 17 1;
245  
246 =head1 SOURCE CODE REPOSITORY
247  
248 L<https://github.com/perlmol/Chemistry-Mol>
249  
250 =head1 SEE ALSO
251  
252 L<Chemistry::Atom>, L<Chemistry::Bond>, L<Chemistry::Mol>
253  
254 =head1 AUTHOR
255  
256 Ivan Tubert-Brohman E<lt>itub@cpan.orgE<gt>
257  
258 =head1 COPYRIGHT
259  
260 Copyright (c) 2005 Ivan Tubert-Brohman. All rights reserved. This program is
261 free software; you can redistribute it and/or modify it under the same terms as
262 Perl itself.
263  
264 =cut
265  
« File/Formula.pm ↑ Summary File/Dumper.pm »