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