Ivan Tubert-Brohman


Chemistry::Obj - Abstract chemistry object


    package MyObj;
    use base "Chemistry::Obj";
    Chemistry::Obj::accessor('color', 'flavor');

    package main;
    my $obj = MyObj->new(name => 'bob', color => 'red');
    $obj->attr(size => 42);
    my $color = $obj->color;
    my $size = $obj->attr('size');


This module implements some generic methods that are used by Chemistry::Mol, Chemistry::Atom, Chemistry::Bond, Chemistry::File, etc.

Common Attributes

There are some common attributes that may be found in molecules, bonds, and atoms, such as id, name, and type. They are all accessed through the methods of the same name. For example, to get the id, call $obj->id; to set the id, call $obj->id('new_id').


Objects should have a unique ID. The user has the responsibility for uniqueness if he assigns ids; otherwise a unique ID is assigned sequentially.


An arbitrary name for an object. The name doesn't need to be unique.


The interpretation of this attribute is not specified here, but it's typically used for bond orders and atom types.


A space where the user can store any kind of information about the object. The accessor method for attr expects the attribute name as the first parameter, and (optionally) the new value as the second parameter. It can also take a hash or hashref with several attributes. Examples:

    $color = $obj->attr('color');
    $obj->attr(color => 'red');
    $obj->attr(color => 'red', flavor => 'cherry');
    $obj->attr({color => 'red', flavor => 'cherry'});



Delete an attribute.

$class->new(name => value, name => value...)

Generic object constructor. It will automatically call each "name" method with the parameter "value". For example,

    $bob = Chemistry::Obj->new(name => 'bob', attr => {size => 42});

is equivalent to

    $bob = Chemistry::Obj->new;
    $bob->attr({size => 42});


Chemistry::Obj overloads a couple of operators for convenience.


The stringification operator. Stringify an object as its id. For example, If an object $obj has the id 'a1', print "$obj" will print 'a1' instead of something like 'Chemistry::Obj=HASH(0x810bbdc)'. If you really want to get the latter, you can call overload::StrVal($obj). See overload for details.


Compare objects by ID. This automatically overloads eq, ne, lt, le, gt, and ge as well. For example, $obj1 eq $obj2 returns true if both objects have the same id, even if they are different objects with different memory addresses. In contrast, $obj1 == $obj2 will return true only if $obj1 and $obj2 point to the same object, with the same memory address.




Chemistry::Atom, Chemistry::Bond, Chemistry::Mol

The PerlMol website http://www.perlmol.org/


Ivan Tubert-Brohman <itub@cpan.org>


Copyright (c) 2005 Ivan Tubert-Brohman. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.