
Chemistry::File::Formula - Molecular formula reader/formatter

use Chemistry::File::Formula;
my $mol = Chemistry::Mol->parse("H2O");
print $mol->print(format => formula);
print $mol->formula; # this is a shorthand for the above
print $mol->print(format => formula,
formula_format => "%s%d{<sub>%d</sub>});

This module converts a molecule object to a string with the formula. It registers the 'formula' format with Chemistry::Mol. Besides its obvious use, it is included in the Chemistry::Mol distribution because it is a very simple example of a Chemistry::File derived I/O module.
The format can be specified as a printf-like string with the following control sequences, which are specified with the formula_format parameter to $mol->print or $mol->write.
If no format is specified, the default is "%s%d". Some examples follow. Let's assume that the formula is C2H6O, as it would be formatted by default.
%s%DLike the default, but include explicit indices for all atoms. The formula would be formatted as "C2H6O1"
%s%d{<sub>%d</sub>}HTML format. The output would be "C<sub>2</sub>H<sub>6</sub>O".
%D %s%j{, }Use a comma followed by a space as a joiner. The output would be "2 C, 6 H, 1 O".
Formulas can also be parsed back into Chemistry::Mol objects. The formula may have parentheses and square or triangular brackets, and it may have the following abbreviations:
Me => '(CH3)',
Et => '(CH3CH2)',
Bu => '(C4H9)',
Bn => '(C6H5CH2)',
Cp => '(C5H5)',
Ph => '(C6H5)',
Bz => '(C6H5CO)',
The formula may also be preceded by a number, which multiplies the whole formula. Some examples of valid formulas:
C5]4)3. Equivalent to C152 (kind of silly example...).
When a formula is parsed, a molecule object is created which consists of the set of the atoms in the formula (no bonds or coordinates, of course). The atoms are created in alphabetical order, so the molecule object for C2H5Br would have the atoms in the following sequence: Br, C, C, H, H, H, H, H.

0.26

Chemistry::Mol, Chemistry::File
The PerlMol website http://www.perlmol.org/

Ivan Tubert-Brohman <itub@cpan.org>.
Formula parsing code contributed by Brent Gregersen.