logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

Chemistry::Obj - Abstract chemistry object

Author

       Ivan Tubert-Brohman <itub@cpan.org>

Description

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

   CommonAttributes
       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')".

       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.

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

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

       attr
           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'});

Name

       Chemistry::Obj - Abstract chemistry object

Operator Overloading

       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.

       cmp 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.

Other Methods

       $obj->del_attr($attr_name)
           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->name('bob');
               $bob->attr({size => 42});

See Also

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

Source Code Repository

       <https://github.com/perlmol/Chemistry-Mol>

Synopsis

           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);
           $obj->color('blue');
           my $color = $obj->color;
           my $size = $obj->attr('size');

See Also