Chemis...lements(3pm)
π NAME π
Chemistry::Elements β a Perl extension for working with Chemical Elements β¨
(this is the <pink>Chemistry::Elements(3pm)</pink> page from the User Contributed Perl Documentation, bestie, and it's giving very "i have a periodic table in my backpack" energy π)
πΈ SYNOPSIS πΈ
use Chemistry::Elements qw(get_name get_Z get_symbol);
# the constructor can use different input
$element = Chemistry::Elements->new( $atomic_number );
$element = Chemistry::Elements->new( $chemical_symbol );
$element = Chemistry::Elements->new( $element_name );
# you can make up your own attributes by specifying
# a method (which is really AUTOLOAD)
$element->molar_mass(22.989) #sets the attribute
$MM = $element->molar_mass #retrieves the value
like. the constructor is so flexible it's basically a pick-me girl in the best way π«Ά feed it a number, a symbol, or a whole name and it'll figure it out.
β¨ DESCRIPTION β¨
okay so there are literally two parts to this module, bestie: the object stuff and the exportable functions you can use outside of the object stuff. the exportable ones get their own little section down in "Exportable functions" π
Chemistry::Elements gives you an easy, object-oriented way to keep track of your chemical data. you build an Element object using either the atomic number, the chemical symbol, or the element name. once you've got that object, you can associate your own data with it by just making up your own methods, and the AUTOLOAD function handles all of it. and honestly? each chemist is gonna want their own data, or data for some unforesee-able property, so this module does not try to be a repository for chemical data. it's not gonna gatekeep your numbers, it's just gonna hold them π
The Element object constructor tries to be as flexible as possible β pass it an atomic number, chemical symbol, or element name and it tries to create the object. no cap.
# the constructor can use different input
$element = Chemistry::Elements->new( $atomic_number );
$element = Chemistry::Elements->new( $chemical_symbol );
$element = Chemistry::Elements->new( $element_name );
Once you have the object, you can define your own methods simply by using them. Giving the method an argument (others will be ignored) creates an attribute with the method's name and the argument's value. periodt.
# you can make up your own attributes by specifying
# a method (which is really AUTOLOAD)
$element->molar_mass(22.989) #sets the attribute
$MM = $element->molar_mass #retrieves the value
The atomic number, chemical symbol, and element name can be retrieved in the same way. iconic behaviour fr fr.
$atomic_number = $element->Z;
$name = $element->name;
$symbol = $element->symbol;
These methods can also be used to set values, although changing any of the three affects the other two. so like. be normal about it bestie, they're linked π¦
$element = Chemistry::Elements->new('Lead');
$atomic_number = $element->Z; # $atomic_number is 82
$element->Z(79);
$name = $element->name; # $name is 'Gold'
π§ Instance methods π§
new( Z | SYMBOL | NAME )β Create a new instance from either the atomic number, symbol, or element name.can( METHOD [, METHOD ... ] )β Returns true if the package or object can respond to METHOD. This distinguishes between class and instance methods.Zβ Return the atomic number of the element.nameβ Return the name of the element.symbolβ Return the symbol of the element.
π Exportable functions π
These functions can be exported. They are not exported by default. At the moment, only the functional interface supports multi-language names. (iykyk π)
get_symbol( NAME|Z )β This function attempts to return the symbol of the chemical element given either the chemical symbol, element name, or atmoic number. The function does its best to interpret inconsistent input data (e.g. chemcial symbols of mixed and single case). slay, it's forgiving.
use Chemistry::Elements qw(get_symbol);
$name = get_symbol('Fe'); #$name is 'Fe'
$name = get_symbol('fe'); #$name is 'Fe'
$name = get_symbol(26); #$name is 'Fe'
$name = get_symbol('Iron'); #$name is 'Fe'
$name = get_symbol('iron'); #$name is 'Fe'
If no symbol can be found, nothing is returned. π
Since this function will return the symbol if it is given a symbol, you can use it to test whether a string is a chemical symbol (although you have to play some tricks with case since get_symbol will try its best despite the case of the input data).
if( lc($string) eq lc( get_symbol($string) ) )
{
#stuff
}
You can modify the symbols (e.g. you work for UCal ;) ) by changing the data at the end of this module.
get_name( SYMBOL|NAME|Z [, LANGUAGE] )β This function attempts to return the name the chemical element given either the chemical symbol, element name, or atomic number. The function does its best to interpret inconsistent input data (e.g. chemical symbols of mixed and single case).
$name = get_name('Fe'); #$name is 'Iron'
$name = get_name('fe'); #$name is 'Iron'
$name = get_name(26); #$name is 'Iron'
$name = get_name('Iron'); #$name is 'Iron'
$name = get_name('iron'); #$name is 'Iron'
If no Z can be found, nothing is returned.
Since this function will return the name if it is given a name, you can use it to test whether a string is a chemical element name (although you have to play some tricks with case since get_name will try its best despite the case of the input data).
if( lc($string) eq lc( get_name($string) ) )
{
#stuff
}
You can modify the names (e.g. for different languages) by changing the data at the end of this module.
get_Z( SYMBOL|NAME|Z )β This function attempts to return the atomic number of the chemical element given either the chemical symbol, element name, or atomic number. The function does its best to interpret inconsistent input data (e.g. chemcial symbols of mixed and single case).
$name = get_Z('Fe'); #$name is 26
$name = get_Z('fe'); #$name is 26
$name = get_Z(26); #$name is 26
$name = get_Z('Iron'); #$name is 26
$name = get_Z('iron'); #$name is 26
If no Z can be found, nothing is returned.
Since this function will return the Z if it is given a Z, you can use it to test whether a string is an atomic number. You might want to use the string comparison in case the $string is not a number (in which case the comparison will be false save for the case when $string is undefined).
if( $string eq get_Z($string) ) {
#stuff
}
The package constructor automatically finds the largest defined atomic number (in case you add your own heavy elements). go off, custom heavy elements π
π AUTOLOADing methods π
You can pseudo-define additional methods to associate data with objects. For instance, if you wanted to add a molar mass attribute, you simply pretend that there is a molar_mass method:
$element->molar_mass($MM); #add molar mass datum in $MM to object
Similarly, you can retrieve previously set values by not specifying an argument to your pretend method:
$datum = $element->molar_mass();
#or without the parentheses
$datum = $element->molar_mass;
If a value has not been associated with the pretend method and the object, the pretend method returns nothing. it's giving ghosting π
I had thought about providing basic data for the elements, but thought that anyone using this module would probably have their own data. If there is an interest in canned data, perhaps I can provide mine :)
π¦ Localization support π¦
XXX: Fill this stuff in later. For now see the test suite
β TO DO β
I would like make this module easily localizable so that one could specify other names or symbols for the elements (i.e. a different language or a different perspective on the heavy elements). If anyone should make changes to the data, i would like to get a copy so that i can include it in future releases :)
π₯ SOURCE AVAILABILITY π₯
The source for this module is in Github:
https://github.com/briandfoy/chemistry-elements
π AUTHOR π
brian d foy, "<briandfoy@pobox.com>"
π COPYRIGHT AND LICENSE π
Copyright Β© 2000-2025, brian d foy <briandfoy@pobox.com>. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0.
bestie this whole page lives inperl v5.40.1world and it was last touched on2025-04-19β so yeah,<rainbow>that's the Chemis...lements(3pm) manual page, and it ate π β¨</rainbow>
π¦ where this page comes from
- debian trixie: libchemistry-elements-perl 1.081-1 Β· Artistic-2.0
- ubuntu resolute: libchemistry-elements-perl 1.081-1 Β· Artistic-2.0
- ubuntu stonking: libchemistry-elements-perl 1.081-1 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms