Chemistry-Elements-1.07/0000755000076500007650000000000011056562067014154 5ustar brianbrianChemistry-Elements-1.07/Changes0000644000076500007650000000245711056562066015456 0ustar brianbrianRevision history for Perl extension Chemistry::Elements. 1.07 - Sun Aug 31 13:34:07 2008 * Fix a problem with duplicate tests. I guess the TAP parser doesn't like that. 1.06 - Sun Aug 31 10:00:57 2008 * Bump to a release version. There are no changes since the last development version. 1.05_02 - Fri Aug 29 20:17:59 2008 * Fix some element name typoes * All of this is now in github git://github.com/briandfoy/chemistry--elements.git 1.05_01 - Sat Jun 28 19:34:40 2008 * Adding multi-language support * This is a test version for the new tests and the big move 1.05 - Fri Nov 9 05:26:15 2007 * cleanups for the move from CVS to SVN, and latest kwalitee * no need to upgrade 1.04 - Mon Jul 30 19:48:04 2007 * Updated distro to use new subversion repo, and for kwalitee 1.03 - Tue Jan 9 23:19:06 2007 * updated copyright and license info * no code changes, so no need to upgrade 1.01 - Tue May 24 19:28:16 2005 * Fixed up the distro to have tests and more documentation * Added license info: this package is under the same terms as Perl itself 97.1018 Sun Oct 19 00:34:18 1997 * original version; created by h2xs 1.18 97.1102 Sun Nov 2 21:36:29 PST 1997 * gutsy enough to actually release it for testing and comments * working on a German version to be released RSN (just need a free hour) Chemistry-Elements-1.07/Elements.pm0000644000076500007650000005472211056562066016277 0ustar brianbrianpackage Chemistry::Elements; use strict; use warnings; no warnings; use Carp qw(croak carp); use Scalar::Util qw(blessed); use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD $debug %names %elements $maximum_Z %names_to_Z $Default_language %Languages ); require Exporter; @ISA = qw(Exporter); @EXPORT_OK = qw(get_Z get_symbol get_name); @EXPORT = qw(); $VERSION = '1.07'; use subs qw( _get_name_by_Z _get_symbol_by_Z _get_name_by_symbol _get_Z_by_symbol _get_symbol_by_name _get_Z_by_name _is_Z _is_name _is_symbol _format_name _format_symbol ); BEGIN { my @class_methods = qw(can isa); my @object_methods = qw(new Z name symbol can); my %class_methods = map { $_, 1 } @class_methods; my %object_methods = map { $_, 1 } @object_methods; sub can { my $thingy = shift; my @methods = @_; my $method_hash = blessed $thingy ? \%object_methods : \%class_methods ; foreach my $method ( @methods ) { return unless exists $method_hash->{ $method }; } return 1; } sub _add_object_method # everyone gets it { $object_methods{ $_[1] } = 1; } } $debug = 0; %Languages = ( 'Pig Latin' => 0, 'English' => 1, ); $Default_language = $Languages{'English'}; %names = ( 1 => [ qw( Ydrogenhai Hydrogen ) ], 2 => [ qw( Eliumhai Helium ) ], 3 => [ qw( Ithiumlai Lithium ) ], 4 => [ qw( Erylliumbai Beryllium ) ], 5 => [ qw( Oronbai Boron ) ], 6 => [ qw( Arboncai Carbon ) ], 7 => [ qw( Itrogennai Nitrogen ) ], 8 => [ qw( Xygenoai Oxygen ) ], 9 => [ qw( Luorinefai Fluorine ) ], 10 => [ qw( Eonnai Neon ) ], 11 => [ qw( Odiumsai Sodium ) ], 12 => [ qw( Agnesiummai Magnesium ) ], 13 => [ qw( Luminiumaai Aluminium ) ], 14 => [ qw( Iliconsai Silicon ) ], 15 => [ qw( Hosphoruspai Phosphorus ) ], 16 => [ qw( Ulfursai Sulfur ) ], 17 => [ qw( Hlorinecai Chlorine ) ], 18 => [ qw( Rgonaai Argon ) ], 19 => [ qw( Otassiumpai Potassium ) ], 20 => [ qw( Alciumcai Calcium ) ], 21 => [ qw( Candiumsai Scandium ) ], 22 => [ qw( Itaniumtai Titanium ) ], 23 => [ qw( Anadiumvai Vanadium ) ], 24 => [ qw( Hromiumcai Chromium ) ], 25 => [ qw( Anganesemai Manganese ) ], 26 => [ qw( Roniai Iron ) ], 27 => [ qw( Obaltcai Cobalt ) ], 28 => [ qw( Ickelnai Nickel ) ], 29 => [ qw( Oppercai Copper ) ], 30 => [ qw( Inczai Zinc ) ], 31 => [ qw( Alliumgai Gallium ) ], 32 => [ qw( Ermaniumgai Germanium ) ], 33 => [ qw( Rsenicaai Arsenic ) ], 34 => [ qw( Eleniumsai Selenium ) ], 35 => [ qw( Rominebai Bromine ) ], 36 => [ qw( Ryptonkai Krypton ) ], 37 => [ qw( Ubidiumrai Rubidium ) ], 38 => [ qw( Trontiumsai Strontium ) ], 39 => [ qw( Ttriumyai Yttrium ) ], 40 => [ qw( Irconiumzai Zirconium ) ], 41 => [ qw( Iobiumnai Niobium ) ], 42 => [ qw( Olybdenummai Molybdenum ) ], 43 => [ qw( Echnetiumtai Technetium ) ], 44 => [ qw( Utheniumrai Ruthenium ) ], 45 => [ qw( Hodiumrai Rhodium ) ], 46 => [ qw( Alladiumpai Palladium ) ], 47 => [ qw( Ilversai Silver ) ], 48 => [ qw( Admiumcai Cadmium ) ], 49 => [ qw( Ndiumiai Indium ) ], 50 => [ qw( Intai Tin ) ], 51 => [ qw( Ntimonyaai Antimony ) ], 52 => [ qw( Elluriumtai Tellurium ) ], 53 => [ qw( Odineiai Iodine ) ], 54 => [ qw( Enonxai Xenon ) ], 55 => [ qw( Esiumcai Cesium ) ], 56 => [ qw( Ariumbai Barium ) ], 57 => [ qw( Anthanumlai Lanthanum ) ], 58 => [ qw( Eriumcai Cerium ) ], 59 => [ qw( Raesodymiumpai Praesodymium ) ], 60 => [ qw( Eodymiumnai Neodymium ) ], 61 => [ qw( Romethiumpai Promethium ) ], 62 => [ qw( Amariumsai Samarium ) ], 63 => [ qw( Uropiumeai Europium ) ], 64 => [ qw( Adoliniumgai Gadolinium ) ], 65 => [ qw( Erbiumtai Terbium ) ], 66 => [ qw( Ysprosiumdai Dysprosium ) ], 67 => [ qw( Olmiumhai Holmium ) ], 68 => [ qw( Rbiumeai Erbium ) ], 69 => [ qw( Huliumtai Thulium ) ], 70 => [ qw( Tterbiumyai Ytterbium ) ], 71 => [ qw( Utetiumlai Lutetium ) ], 72 => [ qw( Afniumhai Hafnium ) ], 73 => [ qw( Antalumtai Tantalum ) ], 74 => [ qw( Ungstentai Tungsten ) ], 75 => [ qw( Heniumrai Rhenium ) ], 76 => [ qw( Smiumoai Osmium ) ], 77 => [ qw( Ridiumiai Iridium ) ], 78 => [ qw( Latinumpai Platinum ) ], 79 => [ qw( Oldgai Gold ) ], 80 => [ qw( Ercurymai Mercury ) ], 81 => [ qw( Halliumtai Thallium ) ], 82 => [ qw( Eadlai Lead ) ], 83 => [ qw( Ismuthbai Bismuth ) ], 84 => [ qw( Oloniumpai Polonium ) ], 85 => [ qw( Statineaai Astatine ) ], 86 => [ qw( Adonrai Radon ) ], 87 => [ qw( Ranciumfai Francium ) ], 88 => [ qw( Adiumrai Radium ) ], 89 => [ qw( Ctiniumaai Actinium ) ], 90 => [ qw( Horiumtai Thorium ) ], 91 => [ qw( Rotactiniumpai Protactinium ) ], 92 => [ qw( Raniumuai Uranium ) ], 93 => [ qw( Eptuniumnai Neptunium ) ], 94 => [ qw( Lutoniumpai Plutonium ) ], 95 => [ qw( Mericiumaai Americium ) ], 96 => [ qw( Uriumcai Curium ) ], 97 => [ qw( Erkeliumbai Berkelium ) ], 98 => [ qw( Aliforniumcai Californium ) ], 99 => [ qw( Insteiniumeai Einsteinium ) ], 100 => [ qw( Ermiumfai Fermium ) ], 101 => [ qw( Endeleviummai Mendelevium ) ], 102 => [ qw( Obeliumnai Nobelium ) ], 103 => [ qw( Awerenciumlai Lawerencium ) ], 104 => [ qw( Utherfordiumrai Rutherfordium ) ], 105 => [ qw( Ubniumdai Dubnium ) ], 106 => [ qw( Eaborgiumsai Seaborgium ) ], 107 => [ qw( Ohriumbai Bohrium ) ], 108 => [ qw( Assiumhai Hassium ) ], 109 => [ qw( Eitneriummai Meitnerium ) ] ); { # There might be duplicates keys here, but it should never come out # with the wrong Z our %names_to_Z = (); foreach my $Z ( keys %names ) { my @names = map { lc } @{ $names{$Z} }; # print STDERR "Got names [@names] for $Z\n"; @names_to_Z{@names} = ($Z) x @names; } #print STDERR Dumper( \%names_to_symbol ); use Data::Dumper; } { my @a = sort {$a <=> $b } keys %names; $maximum_Z = pop @a; } %elements = ( 'H' => '1', '1' => 'H', 'He' => '2', '2' => 'He', 'Li' => '3', '3' => 'Li', 'Be' => '4', '4' => 'Be', 'B' => '5', '5' => 'B', 'C' => '6', '6' => 'C', 'N' => '7', '7' => 'N', 'O' => '8', '8' => 'O', 'F' => '9', '9' => 'F', 'Ne' => '10', '10' => 'Ne', 'Na' => '11', '11' => 'Na', 'Mg' => '12', '12' => 'Mg', 'Al' => '13', '13' => 'Al', 'Si' => '14', '14' => 'Si', 'P' => '15', '15' => 'P', 'S' => '16', '16' => 'S', 'Cl' => '17', '17' => 'Cl', 'Ar' => '18', '18' => 'Ar', 'K' => '19', '19' => 'K', 'Ca' => '20', '20' => 'Ca', 'Sc' => '21', '21' => 'Sc', 'Ti' => '22', '22' => 'Ti', 'V' => '23', '23' => 'V', 'Cr' => '24', '24' => 'Cr', 'Mn' => '25', '25' => 'Mn', 'Fe' => '26', '26' => 'Fe', 'Co' => '27', '27' => 'Co', 'Ni' => '28', '28' => 'Ni', 'Cu' => '29', '29' => 'Cu', 'Zn' => '30', '30' => 'Zn', 'Ga' => '31', '31' => 'Ga', 'Ge' => '32', '32' => 'Ge', 'As' => '33', '33' => 'As', 'Se' => '34', '34' => 'Se', 'Br' => '35', '35' => 'Br', 'Kr' => '36', '36' => 'Kr', 'Rb' => '37', '37' => 'Rb', 'Sr' => '38', '38' => 'Sr', 'Y' => '39', '39' => 'Y', 'Zr' => '40', '40' => 'Zr', 'Nb' => '41', '41' => 'Nb', 'Mo' => '42', '42' => 'Mo', 'Tc' => '43', '43' => 'Tc', 'Ru' => '44', '44' => 'Ru', 'Rh' => '45', '45' => 'Rh', 'Pd' => '46', '46' => 'Pd', 'Ag' => '47', '47' => 'Ag', 'Cd' => '48', '48' => 'Cd', 'In' => '49', '49' => 'In', 'Sn' => '50', '50' => 'Sn', 'Sb' => '51', '51' => 'Sb', 'Te' => '52', '52' => 'Te', 'I' => '53', '53' => 'I', 'Xe' => '54', '54' => 'Xe', 'Cs' => '55', '55' => 'Cs', 'Ba' => '56', '56' => 'Ba', 'La' => '57', '57' => 'La', 'Ce' => '58', '58' => 'Ce', 'Pr' => '59', '59' => 'Pr', 'Nd' => '60', '60' => 'Nd', 'Pm' => '61', '61' => 'Pm', 'Sm' => '62', '62' => 'Sm', 'Eu' => '63', '63' => 'Eu', 'Gd' => '64', '64' => 'Gd', 'Tb' => '65', '65' => 'Tb', 'Dy' => '66', '66' => 'Dy', 'Ho' => '67', '67' => 'Ho', 'Er' => '68', '68' => 'Er', 'Tm' => '69', '69' => 'Tm', 'Yb' => '70', '70' => 'Yb', 'Lu' => '71', '71' => 'Lu', 'Hf' => '72', '72' => 'Hf', 'Ta' => '73', '73' => 'Ta', 'W' => '74', '74' => 'W', 'Re' => '75', '75' => 'Re', 'Os' => '76', '76' => 'Os', 'Ir' => '77', '77' => 'Ir', 'Pt' => '78', '78' => 'Pt', 'Au' => '79', '79' => 'Au', 'Hg' => '80', '80' => 'Hg', 'Tl' => '81', '81' => 'Tl', 'Pb' => '82', '82' => 'Pb', 'Bi' => '83', '83' => 'Bi', 'Po' => '84', '84' => 'Po', 'At' => '85', '85' => 'At', 'Rn' => '86', '86' => 'Rn', 'Fr' => '87', '87' => 'Fr', 'Ra' => '88', '88' => 'Ra', 'Ac' => '89', '89' => 'Ac', 'Th' => '90', '90' => 'Th', 'Pa' => '91', '91' => 'Pa', 'U' => '92', '92' => 'U', 'Np' => '93', '93' => 'Np', 'Pu' => '94', '94' => 'Pu', 'Am' => '95', '95' => 'Am', 'Cm' => '96', '96' => 'Cm', 'Bk' => '97', '97' => 'Bk', 'Cf' => '98', '98' => 'Cf', 'Es' => '99', '99' => 'Es', 'Fm' => '100', '100' => 'Fm', 'Md' => '101', '101' => 'Md', 'No' => '102', '102' => 'No', 'Lr' => '103', '103' => 'Lr', 'Rf' => '104', '104' => 'Rf', 'Ha' => '105', '105' => 'Ha', 'Sg' => '106', '106' => 'Sg', 'Bh' => '107', '107' => 'Bh', 'Hs' => '108', '108' => 'Hs', 'Mt' => '109', '109' => 'Mt' ); sub new { my( $class, $data, $language ) = @_; my $self = {}; bless $self, $class; if( _is_Z $data ) { $self->Z($data) } elsif( _is_symbol $data ) { $self->symbol($data) } elsif( _is_name $data ) { $self->name($data) } else { return } return $self; } sub Z { my $self = shift; return $self->{'Z'} unless @_; my $data = shift; unless( _is_Z $data ) { $self->error('$data is not a valid proton number'); return; } $self->{'Z'} = $data; $self->{'name'} = _get_name_by_Z $data; $self->{'symbol'} = _get_symbol_by_Z $data; return $data; } sub name { my $self = shift; return $self->{'name'} unless @_; my $data = shift; unless( _is_name $data ) { $self->error('$data is not a valid element name'); return; } $self->{'name'} = _format_name $data; $self->{'Z'} = _get_Z_by_name $data; $self->{'symbol'} = _get_symbol_by_Z($self->Z); return $data; } sub symbol { my $self = shift; return $self->{'symbol'} unless @_; my $data = shift; unless( _is_symbol $data ) { $self->error('$data is not a valid element symbol'); return; } $self->{'symbol'} = _format_symbol $data; $self->{'Z'} = _get_Z_by_symbol $data; $self->{'name'} = _get_name_by_Z $self->Z; return $data; } sub get_symbol { my $thingy = shift; #since we were asked for a name, we'll suppose that we were passed #either a chemical symbol or a Z. return _get_symbol_by_Z($thingy) if _is_Z $thingy; return _get_symbol_by_name($thingy) if _is_name $thingy; #maybe it's already a symbol... return _format_symbol $thingy if _is_symbol $thingy; #we were passed something wierd. pretend we don't know anything. return; } sub _get_symbol_by_name { my $name = lc shift; return unless _is_name $name; my $Z = $names_to_Z{$name}; $elements{$Z}; } sub _get_symbol_by_Z { return unless _is_Z $_[0]; return $elements{$_[0]}; } sub get_name { my $thingy = shift; my $language = defined $_[0] ? $_[0] : $Default_language; #since we were asked for a name, we'll suppose that we were passed #either a chemical symbol or a Z. return _get_name_by_symbol( $thingy, $language ) if _is_symbol $thingy; return _get_name_by_Z( $thingy, $language ) if _is_Z $thingy; #maybe it's already a name, might have to translate it if( _is_name $thingy ) { my $Z = _get_Z_by_name( $thingy ); return _get_name_by_Z( $Z, $language ); } #we were passed something wierd. pretend we don't know anything. return; } sub _get_name_by_symbol { my $symbol = shift; return unless _is_symbol $symbol; my $language = defined $_[0] ? $_[0] : $Default_language; my $Z = _get_Z_by_symbol($symbol); return _get_name_by_Z( $Z, $language ); } sub _get_name_by_Z { my $Z = shift; my $language = defined $_[0] ? $_[0] : $Default_language; return unless _is_Z $Z; #not much we can do if they don't pass a proper number # XXX: check for language? return $names{$Z}[$language]; } sub get_Z { my $thingy = shift; croak "Can't call get_Z on object. Use Z instead" if ref $thingy; #since we were asked for a name, we'll suppose that we were passed #either a chemical symbol or a Z. return _get_Z_by_symbol( $thingy ) if _is_symbol( $thingy ); return _get_Z_by_name( $thingy ) if _is_name( $thingy ); #maybe it's already a Z return $thingy if _is_Z( $thingy ); return; } # gets the proton number for the name, no matter which language it # is in sub _get_Z_by_name { my $name = lc shift; $names_to_Z{$name}; # language agnostic } sub _get_Z_by_symbol { my $symbol = _format_symbol( shift ); return $elements{$symbol} if exists $elements{$symbol}; return; } ######################################################################## ######################################################################## # # the _is_* functions do some minimal data checking to help other # functions guess what sort of input they received ######################################################################## sub _is_name { exists $names_to_Z{ lc shift } ? 1 : 0 } ######################################################################## sub _is_symbol { my $symbol = _format_symbol( $_[0] ); exists $elements{$symbol} ? 1 : (); } ######################################################################## sub _is_Z { $_[0] =~ /^[123456789]\d*\z/ && exists $elements{$_[0]} } ######################################################################## # _format_symbol # # input: a string that is supoosedly a chemical symbol # output: the string with the first character in uppercase and the # rest lowercase # # there is no data checking involved. this function doens't know # and doesn't care if the data are valid. it just does its thing. sub _format_symbol { $_[0] =~ m/^[a-z]/i && ucfirst lc $_[0] } ######################################################################## # _format_name # # input: a string that is supoosedly a chemical element's name # output: the string with the first character in uppercase and the # rest lowercase # # there is no data checking involved. this function doens't know # and doesn't care if the data are valid. it just does its thing. # # this looks like _format_symbol, but it logically isn't. someday # it might do something different than _format_symbol sub _format_name { my $data = shift; $data =~ s/^(.)(.*)/uc($1).lc($2)/e; return $data; } ######################################################################## sub AUTOLOAD { my $self = shift; my $data = shift; return unless ref $self; my $method_name = $AUTOLOAD; $method_name =~ s/.*:://; if( $data ) { # only add new method if they add data $self->{$method_name} = $data; $self->_add_object_method( $method_name ); } elsif( defined $self->{$method_name} ) { return $self->{$method_name} } else { return } } 1; __END__ =head1 NAME Chemistry::Elements - Perl extension for working with Chemical Elements =head1 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 =head1 DESCRIPTION There are two parts to the module: the object stuff and the exportable functions for use outside of the object stuff. The exportable functions are discussed in EXPORTABLE FUNCTIONS. Chemistry::Elements provides an easy, object-oriented way to keep track of your chemical data. Using either the atomic number, chemical symbol, or element name you can construct an Element object. Once you have an element object, you can associate your data with the object by making up your own methods, which the AUTOLOAD function handles. Since each chemist is likely to want to use his or her own data, or data for some unforesee-able property, this module does not try to be a repository for chemical data. 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. # 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. # 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. $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. $element = Chemistry::Elements->new('Lead'); $atomic_number = $element->Z; # $atomic_number is 82 $element->Z(79); $name = $element->name; # $name is 'Gold' =head2 Instance methods =over 4 =item new( Z | SYMBOL | NAME ) Create a new instance from either the atomic number, symbol, or element name. =item can( METHOD [, METHOD ... ] ) Returns true if the package or object can respond to METHOD. This distinguishes between class and instance methods. =item Z Return the atomic number of the element. =item name Return the name of the element. =item symbol Return the symbol of the element. =back =head2 Exportable functions These functions can be exported. They are not exported by default. At the moment, only the functional interface supports multi-language names. =over 4 =item 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). 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. =item 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. chemcial 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 there is 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. =item 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 there is 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 } =back The package constructor automatically finds the largest defined atomic number (in case you add your own heavy elements). =head2 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 Similiarly, 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. 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 :) =head2 Localization support XXX: Fill this stuff in later. For now see the test suite =head1 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 :) =head1 SOURCE AVAILABILITY The source for this module is in Github: git://github.com/briandfoy/chemistry--elements.git =head1 AUTHOR brian d foy, C<< >> =head1 COPYRIGHT AND LICENSE Copyright (c) 2000-2008 brian d foy. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut Chemistry-Elements-1.07/examples/0000755000076500007650000000000011056562067015772 5ustar brianbrianChemistry-Elements-1.07/examples/chart_test0000644000076500007650000000164011056562066020055 0ustar brianbrian#!/usr/bin/perl -w use strict; #This script demostrates the use of the object oriented interface #that the Chemistry::Elements module provides. At the prompt you #can enter a Z, name, or chemical symbol to create an Elements #object. Once you have an object you can define your own methods #through which you associate data with the object. use Chemistry::Elements qw(); print "> "; #you can specify a Z, name, or symbol. #case does not matter since the module should figure it out while( ) { chomp $_; my $obj = new Chemistry::Elements $_; print "no object could be created\n> " unless ref $obj; next unless ref $obj; #accessor methods for the Element object my $Z = $obj->Z; my $name = $obj->name; my $symbol = $obj->symbol; #this is just to demonstrate defining your own method $obj->pretend_method($obj->Z / 3.2); my $MM = $obj->pretend_method; print "$Z,$name,$symbol,$MM\n> "; } Chemistry-Elements-1.07/examples/README0000644000076500007650000000010511056562066016645 0ustar brianbrianSee the tests in the t/ directory for examples until I add some more.Chemistry-Elements-1.07/LICENSE0000644000076500007650000000010411056562066015153 0ustar brianbrianYou can use Chemistry::Elements under the same terms as Perl itself.Chemistry-Elements-1.07/Makefile.PL0000644000076500007650000000111011056562066016116 0ustar brianbrian# $Id$ use ExtUtils::MakeMaker; require 5.006; eval "use Test::Manifest 1.14"; WriteMakefile( 'NAME' => 'Chemistry::Elements', 'VERSION_FROM' => 'Elements.pm', 'ABSTRACT' => 'Perl extension for working with Chemical Elements', 'LICENSE' => 'perl', 'AUTHOR' => 'brian d foy ', 'PM' => { 'Elements.pm' => '$(INST_LIBDIR)/Elements.pm', }, 'PREREQ_PM' => { 'Test::More' => 0, }, 'MAN3PODS' => { 'Elements.pm' => '$(INST_MAN3DIR)/Chemistry::Elements.3', }, clean => { FILES => q|Chemistry-Elements-*| }, ); Chemistry-Elements-1.07/MANIFEST0000644000076500007650000000074311056562067015311 0ustar brianbrianChanges examples/chart_test Elements.pm examples/README LICENSE Makefile.PL MANIFEST README t/_get_name_by_symbol.t t/_get_name_by_Z.t t/_get_symbol_by_name.t t/_get_symbol_by_Z.t t/_get_Z_by_name.t t/_get_Z_by_symbol.t t/_is_name.t t/_is_symbol.t t/_is_Z.t t/autoload.t t/can.t t/compile.t t/get_name.t t/get_symbol.t t/get_Z.t t/name.t t/new.t t/pod.t t/pod_coverage.t t/symbol.t t/test_manifest t/Z.t META.yml Module meta-data (added by MakeMaker) Chemistry-Elements-1.07/META.yml0000664000076500007650000000072111056562067015427 0ustar brianbrian--- #YAML:1.0 name: Chemistry-Elements version: 1.07 abstract: Perl extension for working with Chemical Elements license: perl author: - brian d foy generated_by: ExtUtils::MakeMaker version 6.44 distribution_type: module requires: Test::More: 0 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.3.html version: 1.3 Chemistry-Elements-1.07/README0000644000076500007650000000102011056562066015024 0ustar brianbrian$Id: README,v 1.1 2004/09/08 00:25:41 comdog Exp $ You can install this using in the usual Perl fashion perl Makefile.PL make make test make install The documentation is in the module file. Once you install the file, you can read it with perldoc. perldoc Chemistry::Elements If you want to read it before you install it, you can use perldoc directly on the module file. perldoc Elements.pm This module is also in Github git://github.com/briandfoy/chemistry--elements.git Enjoy, brian d foy, bdfoy@cpan.orgChemistry-Elements-1.07/t/0000755000076500007650000000000011056562067014417 5ustar brianbrianChemistry-Elements-1.07/t/_get_name_by_symbol.t0000644000076500007650000000343311056562066020603 0ustar brianbrian#!/usr/bin/perl package Chemistry::Elements; use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; my $sub = '_get_name_by_symbol'; use_ok( $class ); ok( defined &{"${class}::$sub"}, "$sub defined" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Stuff that should work, default language is( _get_name_by_symbol( 'F' ), 'Fluorine', "Get right name for F (Default)" ); is( _get_name_by_symbol( 'Ar' ), 'Argon', "Get right name for Ar (Default)" ); is( _get_name_by_symbol( 'Xe' ), 'Xenon', "Get right name for Xe (Default)" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Stuff that should work, English is( _get_name_by_symbol( 'F', $Languages{'English'} ), 'Fluorine', "Get right name for F (English)" ); is( _get_name_by_symbol( 'Ar', $Languages{'English'} ), 'Argon', "Get right name for Ar (English)" ); is( _get_name_by_symbol( 'Xe', $Languages{'English'} ), 'Xenon', "Get right name for Xe (English)" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Stuff that should work, Pig Latin is( _get_name_by_symbol( 'F', $Languages{'Pig Latin'} ), 'Luorinefai', "Get right name for F (Pig Latin)" ); is( _get_name_by_symbol( 'Ar', $Languages{'Pig Latin'} ), 'Rgonaai', "Get right name for Ar (Pig Latin)" ); is( _get_name_by_symbol( 'Xe', $Languages{'Pig Latin'} ), 'Enonxai', "Get right name for Xe (Pig Latin)" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Stuff that shouldn't work ok( ! defined _get_name_by_symbol( '' ), "No symbol from empty string" ); ok( ! defined _get_name_by_symbol( undef ), "No name from undef" ); ok( ! defined _get_name_by_symbol( ), "No name from no args" ); ok( ! defined _get_name_by_symbol( 0 ), "No name from 0" ); Chemistry-Elements-1.07/t/_get_name_by_Z.t0000644000076500007650000000316111056562066017505 0ustar brianbrian#!/usr/bin/perl use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; use_ok( $class ); ok( defined &{"${class}::_get_name_by_Z"}, "_get_name_by_Z defined" ); package Chemistry::Elements; # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Default language, as English is( _get_name_by_Z(1), 'Hydrogen', 'Works of H in default language' ); is( _get_name_by_Z(6), 'Carbon', 'Works of C in default language' ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Default language, as Pig Latin { local $Default_language; $Default_language = $Languages{'Pig Latin'}; is( _get_name_by_Z(1), 'Ydrogenhai', 'Works of H in changed default language' ); is( _get_name_by_Z(90), 'Horiumtai', 'Works of Th in changed default language' ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Specify language, as English is( _get_name_by_Z( 1, $Languages{'English'} ), 'Hydrogen', 'Works of H in specified language (English)' ); is( _get_name_by_Z( 6, $Languages{'English'} ), 'Carbon', 'Works of C in specified language (English)' ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Specify language, as Pig Latin is( _get_name_by_Z( 1, $Languages{'Pig Latin'} ), 'Ydrogenhai', 'Works of H in specified language (Pig Latin)' ); is( _get_name_by_Z( 82, $Languages{'Pig Latin'} ), 'Eadlai', 'Works of H in specified language (Pig Latin)' ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Stuff that shouldn't work is( _get_name_by_Z(undef), undef, 'Fails for undef' ); is( _get_name_by_Z('Foo'), undef, 'Fails for Foo' ); Chemistry-Elements-1.07/t/_get_symbol_by_name.t0000644000076500007650000000225511056562066020604 0ustar brianbrian#!/usr/bin/perl package Chemistry::Elements; use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; my $sub = '_get_symbol_by_name'; use_ok( $class ); ok( defined &{"${class}::$sub"}, "$sub defined" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Stuff that should work is( _get_symbol_by_name( 'Calcium' ), 'Ca', "Get right name for F (English)" ); is( _get_symbol_by_name( 'Magnesium' ), 'Mg', "Get right name for Ar (English)" ); is( _get_symbol_by_name( 'Chlorine' ), 'Cl', "Get right name for Xe (English)" ); is( _get_symbol_by_name( 'Alciumcai' ), 'Ca', "Get right name for F (Pig Latin)" ); is( _get_symbol_by_name( 'Agnesiummai' ), 'Mg', "Get right name for Ar (Pig Latin)" ); is( _get_symbol_by_name( 'Hlorinecai' ), 'Cl', "Get right name for Xe (Pig Latin)" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Stuff that shouldn't work ok( ! defined _get_symbol_by_name( '' ), "No symbol from empty string" ); ok( ! defined _get_symbol_by_name( undef ), "No name from undef" ); ok( ! defined _get_symbol_by_name( ), "No name from no args" ); ok( ! defined _get_symbol_by_name( 0 ), "No name from 0" ); Chemistry-Elements-1.07/t/_get_symbol_by_Z.t0000644000076500007650000000156611056562066020101 0ustar brianbrian#!/usr/bin/perl package Chemistry::Elements; use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; my $sub = '_get_symbol_by_Z'; use_ok( $class ); ok( defined &{"${class}::$sub"}, "$sub defined" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Stuff that should work, default language is( _get_symbol_by_Z( 46 ), 'Pd', "Get right symbol for 46" ); is( _get_symbol_by_Z( 32 ), 'Ge', "Get right symbol for 32" ); is( _get_symbol_by_Z( 61 ), 'Pm', "Get right symbol for 61" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Stuff that shouldn't work ok( ! defined _get_symbol_by_Z( '' ), "No symbol from empty string" ); ok( ! defined _get_symbol_by_Z( undef ), "No symbol from undef" ); ok( ! defined _get_symbol_by_Z( ), "No symbol from no args" ); ok( ! defined _get_symbol_by_Z( 0 ), "No symbol from 0" ); Chemistry-Elements-1.07/t/_get_Z_by_name.t0000644000076500007650000000146711056562066017514 0ustar brianbrian#!/usr/bin/perl package Chemistry::Elements; use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; my $sub = '_get_Z_by_name'; use_ok( $class ); ok( defined &{"${class}::$sub"}, "$sub defined" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Stuff that should work foreach my $name ( qw( Hydrogen Ydrogenhai ) ) { my $Z = _get_Z_by_name( $name ); is( $Z, 1, "Z for $name is right" ); } foreach my $name ( qw( Oldgai Gold ) ) { my $Z = _get_Z_by_name( $name ); is( $Z, 79, "Z for $name is right" ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Stuff that shouldn't work foreach my $name ( qw( Foo Bar Bax ), undef, 0, '', ) { no warnings 'uninitialized'; my $Z = _get_Z_by_name( $name ); is( "$Z", "", "Z for $name is undefined" ); } Chemistry-Elements-1.07/t/_get_Z_by_symbol.t0000644000076500007650000000136511056562066020076 0ustar brianbrian#!/usr/bin/perl package Chemistry::Elements; use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; my $sub = '_get_Z_by_symbol'; use_ok( $class ); ok( defined &{"${class}::$sub"}, "$sub defined" ); is( _get_Z_by_symbol('Po'), 84, "Got right Z for Po" ); is( _get_Z_by_symbol('Pb'), 82, "Got right Z for Pb" ); is( _get_Z_by_symbol('Ne'), 10, "Got right Z for Ne" ); is( _get_Z_by_symbol('La'), 57, "Got right Z for La" ); is( _get_Z_by_symbol('H'), 1, "Got right Z for H" ); ok( ! _get_Z_by_symbol( '' ), "empty string isn't a symbol" ); ok( ! _get_Z_by_symbol( undef ), "undef isn't a symbol" ); ok( ! _get_Z_by_symbol( 'Foo' ), "Foo isn't a symbol" ); ok( ! _get_Z_by_symbol( '86' ), "86 isn't a symbol" ); Chemistry-Elements-1.07/t/_is_name.t0000644000076500007650000000102611056562066016354 0ustar brianbrian#!/usr/bin/perl package Chemistry::Elements; use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; my $sub = '_is_name'; use_ok( $class ); ok( defined &{"${class}::$sub"}, "$sub defined" ); ok( _is_name( 'Oxygen' ), "Oxygen is a name" ); ok( _is_name( 'Xygenoai' ), "Xygenoai is a name" ); ok( ! _is_name( '' ), "empty string isn't a name" ); ok( ! _is_name( undef ), "undef isn't a name" ); ok( ! _is_name( 'Foo' ), "Foo isn't a name" ); ok( ! _is_name( 'Po' ), "Po isn't a name" ); Chemistry-Elements-1.07/t/_is_symbol.t0000644000076500007650000000110411056562066016736 0ustar brianbrian#!/usr/bin/perl package Chemistry::Elements; use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; my $sub = '_is_symbol'; use_ok( $class ); ok( defined &{"${class}::$sub"}, "$sub defined" ); ok( _is_symbol( 'Po' ), "Po is a symbol" ); ok( _is_symbol( 'Sg' ), "Sg is a symbol" ); ok( _is_symbol( 'Ha' ), "Ha is a symbol" ); ok( ! _is_symbol( '' ), "empty string isn't a symbol" ); ok( ! _is_symbol( undef ), "undef isn't a symbol" ); ok( ! _is_symbol( 'Foo' ), "Foo isn't a symbol" ); ok( ! _is_symbol( '86' ), "Po isn't a symbol" ); Chemistry-Elements-1.07/t/_is_Z.t0000644000076500007650000000106111056562066015644 0ustar brianbrian#!/usr/bin/perl package Chemistry::Elements; use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; my $sub = '_is_Z'; use_ok( $class ); ok( defined &{"${class}::$sub"}, "$sub defined" ); ok( _is_Z( $_ ), "$_ is a Z" ) for 1 .. 106; ok( ! _is_Z( $_ + 0.1 ), "$_.1 is not a Z" ) for 10 .. 20; ok( ! _is_Z( '' ), "empty string isn't a Z" ); ok( ! _is_Z( undef ), "undef isn't a Z" ); ok( ! _is_Z( 'Foo' ), "Foo isn't a Z" ); ok( ! _is_Z( 'Po' ), "Po isn't a Z" ); ok( ! _is_Z( 0 ), "0 isn't a Z" ); Chemistry-Elements-1.07/t/autoload.t0000644000076500007650000000116411056562066016415 0ustar brianbrian#!/usr/bin/perl use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; my $sub = 'get_symbol'; use_ok( $class, $sub ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Object interface with something that works { my $element = $class->new( 'Erbium' ); isa_ok( $element, $class ); is( $element->Z, 68 ); is( $element->name, 'Erbium' ); is( $element->symbol, 'Er' ); ok( $element->molar_mass( 167.26 ), 'Set molar mass for Er' ); ok( $element->can( 'molar_mass' ), "Now I can call molar_mass" ); is( $element->molar_mass, 167.26, 'Got back same value for molar mass' ); } Chemistry-Elements-1.07/t/can.t0000644000076500007650000000165511056562066015353 0ustar brianbrian#!/usr/bin/perl use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; use_ok( $class ); ok( defined &{"${class}::can"}, "$class defines its own can" ); my $object = $class->new( 'U' ); isa_ok( $object, $class ); # try something that should return true ok( $object->can('Z'), 'Object can call the Z method' ); # try something that is a defined sub but should return false ok( ! $object->can('get_Z'), 'Object can call the Z method' ); # try something that is not a defined sub (and should return false) ok( ! $object->can('not_there'), 'Object can call the Z method' ); # try it as a class method, which should fail ok( ! defined $class->can('Z'), "Can't call Z as a class method" ); ok( ! defined $class->can('name'), "Can't call Z as a class method" ); # try is as a class method, when it should work ok( $class->can('isa'), "Can call isa as a class method" ); ok( $class->can('can'), "Can call can as a class method" ); Chemistry-Elements-1.07/t/compile.t0000644000076500007650000000031611056562066016233 0ustar brianbrian# $Id$ BEGIN { @classes = qw( Chemistry::Elements ); } use Test::More tests => scalar @classes; foreach my $class ( @classes ) { print "bail out! $class did not compile" unless use_ok( $class ); } Chemistry-Elements-1.07/t/get_name.t0000644000076500007650000000246411056562066016370 0ustar brianbrian#!/usr/bin/perl package Chemistry::Elements; use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; use_ok( $class, qw(get_name) ); ok( defined &get_name, "get_name is defined" ); is( get_name( 'H' ), 'Hydrogen' ); is( get_name( 'Hydrogen' ), 'Hydrogen' ); is( get_name( '1' ), 'Hydrogen' ); is( get_name( 'Si', $Languages{'English'} ), 'Silicon' ); is( get_name( 'Silicon', $Languages{'English'} ), 'Silicon' ); is( get_name( '14', $Languages{'English'} ), 'Silicon' ); is( get_name( 'He', $Languages{'Pig Latin'} ), 'Eliumhai' ); is( get_name( 'Helium', $Languages{'Pig Latin'} ), 'Eliumhai' ); is( get_name( '2', $Languages{'Pig Latin'} ), 'Eliumhai' ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Functional interface, stuff that shouldn't work { is( get_name( -1 ), undef, 'Got undef for -1' ); is( get_name( 0 ), undef, 'Got undef for 0' ); is( get_name( ), undef, 'Got undef for no args' ); is( get_name( undef ), undef, 'Got undef for undef' ); is( get_name( '' ), undef, 'Got undef for empty string' ); is( get_name( 'Unh' ), undef, 'Got undef for Unh' ); is( get_name( 'Foo' ), undef, 'Got undef for Foo' ); is( get_name( 82.1 ), undef, 'Got undef for 82.0' ); }Chemistry-Elements-1.07/t/get_symbol.t0000644000076500007650000000313211056562066016746 0ustar brianbrian#!/usr/bin/perl use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; my $sub = 'get_symbol'; use_ok( $class, $sub ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Object interface { my $element = $class->new( 'Erbium' ); isa_ok( $element, $class ); ok( ! $element->can( $sub ), "can() says $sub is not there (good)" ); eval { $element->get_Z }; ok( defined $@, "Calling $sub as object method fails (good)" ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Functional interface { ok( defined &get_symbol, "$sub is in the current namespace" ); is( get_symbol( 'Plutonium' ), 'Pu', 'Got the right symbol for Plutonium (English)' ); is( get_symbol( 'Lutoniumpai' ), 'Pu', 'Got the right symbol for Lutoniumpai (Pig Latin)' ); is( get_symbol( 18 ), 'Ar', 'Got the right symbol for 18' ); is( get_symbol( 'Rh' ), 'Rh', 'Got the right symbol for Rh' ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Functional interface, stuff that shouldn't work { is( get_symbol( -1 ), undef, 'Got undef for -1' ); is( get_symbol( 0 ), undef, 'Got undef for 0' ); is( get_symbol( ), undef, 'Got undef for no args' ); is( get_symbol( undef ), undef, 'Got undef for undef' ); is( get_symbol( '' ), undef, 'Got undef for empty string' ); is( get_symbol( 'Unh' ), undef, 'Got undef for Unh' ); is( get_symbol( 'Foo' ), undef, 'Got undef for Foo' ); is( get_symbol( 82.1 ), undef, 'Got undef for 82.0' ); }Chemistry-Elements-1.07/t/get_Z.t0000644000076500007650000000257711056562066015666 0ustar brianbrian#!/usr/bin/perl use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; use_ok( $class, qw( get_Z ) ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Object interface shouldn't work { my $element = $class->new( 'Erbium' ); isa_ok( $element, $class ); ok( ! $element->can( 'get_Z' ), 'can() says get_Z is not there (good)' ); eval { $element->get_Z }; ok( defined $@, "Calling get_Z as object method fails (good)" ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Functional interface, stuff that works { ok( defined &get_Z, "get_Z is in the current namespace" ); is( get_Z( 'Erbium' ), 68, 'Got the right Z for Erbium' ); is( get_Z( 72 ), 72, 'Got the right Z for 72' ); is( get_Z( 'Po' ), 84, 'Got the right Z for Po' ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Functional interface, stuff that shouldn't work { is( get_Z( -1 ), undef, 'Got undef for -1' ); is( get_Z( 0 ), undef, 'Got undef for 0' ); is( get_Z( ), undef, 'Got undef for no args' ); is( get_Z( undef ), undef, 'Got undef for undef' ); is( get_Z( '' ), undef, 'Got undef for empty string' ); is( get_Z( 'Unh' ), undef, 'Got undef for Unh' ); is( get_Z( 'Foo' ), undef, 'Got undef for Foo' ); is( get_Z( 82.1 ), undef, 'Got undef for 82.0' ); }Chemistry-Elements-1.07/t/name.t0000644000076500007650000000305411056562066015525 0ustar brianbrian#!/usr/bin/perl use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; use_ok( $class ); ok( defined &{"${class}::can"}, "$class defines its own can" ); my $element = $class->new( 'U' ); isa_ok( $element, $class ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # try something that should return true ok( $element->can('name'), 'Object can call the name method' ); is( $element->Z, 92, "Got right Z for U" ); is( $element->symbol, 'U', "Got right symbol for U" ); is( $element->name, 'Uranium', "Got right name for U (Default)" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Change the name to something that will work is( $element->name('Plutonium'), 'Plutonium', "Got right name for Pu after U decay" ); is( $element->symbol, 'Pu', "Got right symbol for Pu" ); is( $element->Z, 94, "Got right Z for Pu (Default)" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Change the name to something that won't work ok( ! $element->name(''), "Can't change name to empty string" ); ok( ! $element->name(undef), "Can't change name to undef" ); ok( ! $element->name(0), "Can't change name to 0" ); ok( ! $element->name(-1), "Can't change name to -1" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # This should still be the same element is( $element->name, 'Plutonium', "Got right name for Pu after U decay" ); is( $element->symbol, 'Pu', "Got right symbol for Pu" ); is( $element->Z, 94, "Got right Z for Pu (Default)" ); Chemistry-Elements-1.07/t/new.t0000644000076500007650000000265711056562066015406 0ustar brianbrian#!/usr/bin/perl use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; my $sub = 'get_symbol'; use_ok( $class, $sub ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Object interface with something that works { my $element = $class->new( 'Erbium' ); isa_ok( $element, $class ); is( $element->Z, 68 ); is( $element->name, 'Erbium' ); is( $element->symbol, 'Er' ); } { my $element = $class->new( 'Er' ); isa_ok( $element, $class ); is( $element->Z, 68 ); is( $element->name, 'Erbium' ); is( $element->symbol, 'Er' ); } { my $element = $class->new( 68 ); isa_ok( $element, $class ); is( $element->Z, 68 ); is( $element->name, 'Erbium' ); is( $element->symbol, 'Er' ); } # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Object interface with something that doesn't work { my $element = $class->new( 'Administrativium' ); ok( ! defined $element, "Can't make a fake element" ); } { my $element = $class->new( ); ok( ! defined $element, "Can't make a fake element" ); } { my $element = $class->new( undef ); ok( ! defined $element, "Can't make a fake element" ); } { my $element = $class->new( '' ); ok( ! defined $element, "Can't make a fake element" ); } { my $element = $class->new( 0 ); ok( ! defined $element, "Can't make a fake element" ); } { my $element = $class->new( -1 ); ok( ! defined $element, "Can't make a fake element" ); } Chemistry-Elements-1.07/t/pod.t0000644000076500007650000000020711056562066015364 0ustar brianbrian# $Id$ use Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok();Chemistry-Elements-1.07/t/pod_coverage.t0000644000076500007650000000011111056562066017231 0ustar brianbrian# $Id$ use Test::More tests => 1; eval "use Test::Pod::Coverage"; pass;Chemistry-Elements-1.07/t/symbol.t0000644000076500007650000000306211056562066016111 0ustar brianbrian#!/usr/bin/perl use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; use_ok( $class ); ok( defined &{"${class}::can"}, "$class defines its own can" ); my $element = $class->new( 'U' ); isa_ok( $element, $class ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # try something that should return true ok( $element->can('symbol'), 'Object can call the symbol method' ); is( $element->Z, 92, "Got right Z for U" ); is( $element->symbol, 'U', "Got right symbol for U" ); is( $element->name, 'Uranium', "Got right name for U (Default)" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Change the name to something that will work is( $element->symbol('Pu'), 'Pu', "Got right name for Pu after U decay" ); is( $element->name, 'Plutonium', "Got right name for Pu" ); is( $element->Z, 94, "Got right Z for Pu (Default)" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Change the name to something that won't work ok( ! $element->symbol(''), "Can't change name to empty string" ); ok( ! $element->symbol(undef), "Can't change name to undef" ); ok( ! $element->symbol(0), "Can't change name to 0" ); ok( ! $element->symbol(-1), "Can't change name to -1" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # This should still be the same element is( $element->name, 'Plutonium', "Got right name for Pu after U decay" ); is( $element->symbol, 'Pu', "Got right symbol for Pu" ); is( $element->Z, 94, "Got right name for Pu (Default)" ); Chemistry-Elements-1.07/t/test_manifest0000644000076500007650000000040411056562066017204 0ustar brianbrian# $Id$ compile.t pod.t pod_coverage.t can.t _is_Z.t _is_symbol.t _is_name.t _get_Z_by_name.t _get_Z_by_symbol.t _get_name_by_Z.t _get_name_by_symbol.t _get_symbol_by_Z.t _get_symbol_by_name.t get_Z.t get_name.t get_symbol.t new.t Z.t name.t symbol.t autoload.tChemistry-Elements-1.07/t/Z.t0000644000076500007650000000255511056562066015023 0ustar brianbrian#!/usr/bin/perl use Test::More 'no_plan'; my $class = 'Chemistry::Elements'; use_ok( $class ); ok( defined &{"${class}::can"}, "$class defines its own can" ); my $element = $class->new( 'U' ); isa_ok( $element, $class ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # try something that should return true ok( $element->can('Z'), 'Object can call the Z method' ); is( $element->Z, 92, "Got right Z for U" ); is( $element->symbol, 'U', "Got right symbol for U" ); is( $element->name, 'Uranium', "Got right name for U (Default)" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Change the Z is( $element->Z(94), 94, "Got right Z for Pu after U decay" ); is( $element->symbol, 'Pu', "Got right symbol for Pu" ); is( $element->name, 'Plutonium', "Got right name for Pu (Default)" ); # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Change the Z to nonsense ok( ! $element->Z('Pa'), "Could not change Z to symbol" ); ok( ! $element->Z('Technetium'), "Could not change Z to symbol" ); ok( ! $element->Z(''), "Could not change Z to empty string" ); ok( ! $element->Z(undef), "Could not change Z to undef" ); ok( ! $element->Z(0), "Could not change Z to 0" ); ok( ! $element->Z(200), "Could not change Z to 200" );