~ubuntu-branches/ubuntu/jaunty/horae/jaunty

« back to all changes in this revision

Viewing changes to 0CPAN/Chemistry-Elements-1.01/Elements.pm

  • Committer: Bazaar Package Importer
  • Author(s): Carlo Segre
  • Date: 2008-02-23 23:13:02 UTC
  • mfrom: (2.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080223231302-mnyyxs3icvrus4ke
Tags: 066-3
Apply patch to athena_parts/misc.pl for compatibility with 
perl-tk 804.28.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package Chemistry::Elements;
2
 
 
3
 
use strict;
4
 
require 5.003;
5
 
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD
6
 
             $debug %names %elements $maximum_Z);
7
 
 
8
 
require Exporter;
9
 
 
10
 
@ISA       = qw(Exporter);
11
 
@EXPORT_OK = qw(get_Z get_symbol get_name);
12
 
@EXPORT    = qw();
13
 
$VERSION   = 1.01;
14
 
 
15
 
use subs qw(_get_name_by_Z
16
 
            _get_symbol_by_Z
17
 
            _get_name_by_symbol
18
 
            _get_Z_by_symbol
19
 
            _get_symbol_by_name
20
 
            _get_Z_by_name
21
 
            _is_Z
22
 
            _is_name
23
 
            _is_symbol
24
 
            _format_name
25
 
            _format_symbol
26
 
            );
27
 
 
28
 
 
29
 
$debug = 0;
30
 
 
31
 
%names =
32
 
(
33
 
  1 => 'Hydrogen',
34
 
  2 => 'Helium',
35
 
  3 => 'Lithium',
36
 
  4 => 'Beryllium',
37
 
  5 => 'Boron',
38
 
  6 => 'Carbon',
39
 
  7 => 'Nitrogen',
40
 
  8 => 'Oxygen',
41
 
  9 => 'Fluorine',
42
 
 10 => 'Neon',
43
 
 11 => 'Sodium',
44
 
 12 => 'Magnesium',
45
 
 13 => 'Aluminium',
46
 
 14 => 'Silicon',
47
 
 15 => 'Phosphorous',
48
 
 16 => 'Sulfur',
49
 
 17 => 'Chlorine',
50
 
 18 => 'Argon',
51
 
 19 => 'Potassium',
52
 
 20 => 'Calcium',
53
 
 21 => 'Scandium',
54
 
 22 => 'Titanium',
55
 
 23 => 'Vanadium',
56
 
 24 => 'Chromium',
57
 
 25 => 'Manganese',
58
 
 26 => 'Iron',
59
 
 27 => 'Cobolt',
60
 
 28 => 'Nickel',
61
 
 29 => 'Copper',
62
 
 30 => 'Zinc',
63
 
 31 => 'Gallium',
64
 
 32 => 'Germanium',
65
 
 33 => 'Arsenic',
66
 
 34 => 'Selenium',
67
 
 35 => 'Bromine',
68
 
 36 => 'Krypton',
69
 
 37 => 'Rubidium',
70
 
 38 => 'Strontium',
71
 
 39 => 'Yttrium',
72
 
 40 => 'Zirconium',
73
 
 41 => 'Nobium',
74
 
 42 => 'Molybdenum',
75
 
 43 => 'Technetium',
76
 
 44 => 'Ruthenium',
77
 
 45 => 'Rhodium',
78
 
 46 => 'Paladium',
79
 
 47 => 'Silver',
80
 
 48 => 'Cadmium',
81
 
 49 => 'Indium',
82
 
 50 => 'Tin',
83
 
 51 => 'Antimony',
84
 
 52 => 'Tellurium',
85
 
 53 => 'Iodine',
86
 
 54 => 'Xenon',
87
 
 55 => 'Cesium',
88
 
 56 => 'Barium',
89
 
 57 => 'Lanthanum',
90
 
 58 => 'Cerium',
91
 
 59 => 'Praesodium',
92
 
 60 => 'Neodymium',
93
 
 61 => 'Promethium',
94
 
 62 => 'Samarium',
95
 
 63 => 'Europium',
96
 
 64 => 'Gadolinium',
97
 
 65 => 'Terbium',
98
 
 66 => 'Dysprosium',
99
 
 67 => 'Holmium',
100
 
 68 => 'Erbium',
101
 
 69 => 'Thulium',
102
 
 70 => 'Ytterbium',
103
 
 71 => 'Lutetium',
104
 
 72 => 'Hafnium',
105
 
 73 => 'Tantalum',
106
 
 74 => 'Tungsten',
107
 
 75 => 'Rhenium',
108
 
 76 => 'Osmium',
109
 
 77 => 'Iridium',
110
 
 78 => 'Platinum',
111
 
 79 => 'Gold',
112
 
 80 => 'Mercury',
113
 
 81 => 'Thalium',
114
 
 82 => 'Lead',
115
 
 83 => 'Bismuth',
116
 
 84 => 'Polonium',
117
 
 85 => 'Astatine',
118
 
 86 => 'Radon',
119
 
 87 => 'Francium',
120
 
 88 => 'Radium',
121
 
 89 => 'Actinum',
122
 
 90 => 'Thorium',
123
 
 91 => 'Protactinium',
124
 
 92 => 'Uranium',
125
 
 93 => 'Neptunium',
126
 
 94 => 'Plutonium',
127
 
 95 => 'Americium',
128
 
 96 => 'Curium',
129
 
 97 => 'Berkelium',
130
 
 98 => 'Californium',
131
 
 99 => 'Einsteinium',
132
 
100 => 'Fermium',
133
 
101 => 'Mendelevium',
134
 
102 => 'Nobelium',
135
 
103 => 'Lawerencium',
136
 
104 => 'Rutherfordium',
137
 
105 => 'Dubnium',
138
 
106 => 'Seaborgium',
139
 
107 => 'Bohrium',
140
 
108 => 'Hassium',
141
 
109 => 'Meitnerium'
142
 
);
143
 
 
144
 
{
145
 
my @a = sort {$a <=> $b } keys %names;
146
 
$maximum_Z = pop @a;
147
 
}
148
 
 
149
 
%elements = (
150
 
'H'  => '1',      '1' => 'H',
151
 
'He' => '2',      '2' => 'He',
152
 
'Li' => '3',      '3' => 'Li',
153
 
'Be' => '4',      '4' => 'Be',
154
 
'B'  => '5',      '5' => 'B',
155
 
'C'  => '6',      '6' => 'C',
156
 
'N'  => '7',      '7' => 'N',
157
 
'O'  => '8',      '8' => 'O',
158
 
'F'  => '9',      '9' => 'F',
159
 
'Ne' => '10',    '10' => 'Ne',
160
 
'Na' => '11',    '11' => 'Na',
161
 
'Mg' => '12',    '12' => 'Mg',
162
 
'Al' => '13',    '13' => 'Al',
163
 
'Si' => '14',    '14' => 'Si',
164
 
'P'  => '15',    '15' => 'P',
165
 
'S'  => '16',    '16' => 'S',
166
 
'Cl' => '17',    '17' => 'Cl',
167
 
'Ar' => '18',    '18' => 'Ar',
168
 
'K'  => '19',    '19' => 'K',
169
 
'Ca' => '20',    '20' => 'Ca',
170
 
'Sc' => '21',    '21' => 'Sc',
171
 
'Ti' => '22',    '22' => 'Ti',
172
 
'V'  => '23',    '23' => 'V',
173
 
'Cr' => '24',    '24' => 'Cr',
174
 
'Mn' => '25',    '25' => 'Mn',
175
 
'Fe' => '26',    '26' => 'Fe',
176
 
'Co' => '27',    '27' => 'Co',
177
 
'Ni' => '28',    '28' => 'Ni',
178
 
'Cu' => '29',    '29' => 'Cu',
179
 
'Zn' => '30',    '30' => 'Zn',
180
 
'Ga' => '31',    '31' => 'Ga',
181
 
'Ge' => '32',    '32' => 'Ge',
182
 
'As' => '33',    '33' => 'As',
183
 
'Se' => '34',    '34' => 'Se',
184
 
'Br' => '35',    '35' => 'Br',
185
 
'Kr' => '36',    '36' => 'Kr',
186
 
'Rb' => '37',    '37' => 'Rb',
187
 
'Sr' => '38',    '38' => 'Sr',
188
 
'Y'  => '39',    '39' => 'Y',
189
 
'Zr' => '40',    '40' => 'Zr',
190
 
'Nb' => '41',    '41' => 'Nb',
191
 
'Mo' => '42',    '42' => 'Mo',
192
 
'Tc' => '43',    '43' => 'Tc',
193
 
'Ru' => '44',    '44' => 'Ru',
194
 
'Rh' => '45',    '45' => 'Rh',
195
 
'Pd' => '46',    '46' => 'Pd',
196
 
'Ag' => '47',    '47' => 'Ag',
197
 
'Cd' => '48',    '48' => 'Cd',
198
 
'In' => '49',    '49' => 'In',
199
 
'Sn' => '50',    '50' => 'Sn',
200
 
'Sb' => '51',    '51' => 'Sb',
201
 
'Te' => '52',    '52' => 'Te',
202
 
'I'  => '53',    '53' => 'I',
203
 
'Xe' => '54',    '54' => 'Xe',
204
 
'Cs' => '55',    '55' => 'Cs',
205
 
'Ba' => '56',    '56' => 'Ba',
206
 
'La' => '57',    '57' => 'La',
207
 
'Ce' => '58',    '58' => 'Ce',
208
 
'Pr' => '59',    '59' => 'Pr',
209
 
'Nd' => '60',    '60' => 'Nd',
210
 
'Pm' => '61',    '61' => 'Pm',
211
 
'Sm' => '62',    '62' => 'Sm',
212
 
'Eu' => '63',    '63' => 'Eu',
213
 
'Gd' => '64',    '64' => 'Gd',
214
 
'Tb' => '65',    '65' => 'Tb',
215
 
'Dy' => '66',    '66' => 'Dy',
216
 
'Ho' => '67',    '67' => 'Ho',
217
 
'Er' => '68',    '68' => 'Er',
218
 
'Tm' => '69',    '69' => 'Tm',
219
 
'Yb' => '70',    '70' => 'Yb',
220
 
'Lu' => '71',    '71' => 'Lu',
221
 
'Hf' => '72',    '72' => 'Hf',
222
 
'Ta' => '73',    '73' => 'Ta',
223
 
'W'  => '74',    '74' => 'W',
224
 
'Re' => '75',    '75' => 'Re',
225
 
'Os' => '76',    '76' => 'Os',
226
 
'Ir' => '77',    '77' => 'Ir',
227
 
'Pt' => '78',    '78' => 'Pt',
228
 
'Au' => '79',    '79' => 'Au',
229
 
'Hg' => '80',    '80' => 'Hg',
230
 
'Tl' => '81',    '81' => 'Tl',
231
 
'Pb' => '82',    '82' => 'Pb',
232
 
'Bi' => '83',    '83' => 'Bi',
233
 
'Po' => '84',    '84' => 'Po',
234
 
'At' => '85',    '85' => 'At',
235
 
'Rn' => '86',    '86' => 'Rn',
236
 
'Fr' => '87',    '87' => 'Fr',
237
 
'Ra' => '88',    '88' => 'Ra',
238
 
'Ac' => '89',    '89' => 'Ac',
239
 
'Th' => '90',    '90' => 'Th',
240
 
'Pa' => '91',    '91' => 'Pa',
241
 
'U'  => '92',    '92' => 'U',
242
 
'Np' => '93',    '93' => 'Np',
243
 
'Pu' => '94',    '94' => 'Pu',
244
 
'Am' => '95',    '95' => 'Am',
245
 
'Cm' => '96',    '96' => 'Cm',
246
 
'Bk' => '97',    '97' => 'Bk',
247
 
'Cf' => '98',    '98' => 'Cf',
248
 
'Es' => '99',    '99' => 'Es',
249
 
'Fm' => '100',  '100' => 'Fm',
250
 
'Md' => '101',  '101' => 'Md',
251
 
'No' => '102',  '102' => 'No',
252
 
'Lr' => '103',  '103' => 'Lr',
253
 
'Rf' => '104',  '104' => 'Rf',
254
 
'Ha' => '105',  '105' => 'Ha',
255
 
'Sg' => '106',  '106' => 'Sg',
256
 
'Bh' => '107',  '107' => 'Bh',
257
 
'Hs' => '108',  '108' => 'Hs',
258
 
'Mt' => '109',  '109' => 'Mt'
259
 
);
260
 
 
261
 
sub new
262
 
        {
263
 
        my $class = shift;
264
 
        my $data  = shift;
265
 
 
266
 
        my $self = {};
267
 
        bless $self, $class;
268
 
 
269
 
        if( _is_Z $data )
270
 
                {
271
 
                $self->Z($data);
272
 
                }
273
 
        elsif( _is_symbol $data )
274
 
                {
275
 
                $self->symbol($data);
276
 
                }
277
 
        elsif( _is_name $data )
278
 
                {
279
 
                $self->name($data);
280
 
                }
281
 
        else
282
 
                {
283
 
                return;
284
 
                }
285
 
 
286
 
        return $self;
287
 
        }
288
 
 
289
 
sub Z
290
 
        {
291
 
        my $self = shift;
292
 
        my $data = shift;
293
 
        
294
 
        return $self->{'Z'} unless $data;
295
 
 
296
 
        unless( _is_Z $data )
297
 
                {
298
 
                $self->error('$data is not a valid proton number');
299
 
                return;
300
 
                }
301
 
 
302
 
        $self->{'Z'}      = $data;
303
 
        $self->{'name'}   = _get_name_by_Z $data;
304
 
        $self->{'symbol'} = _get_symbol_by_Z $data;
305
 
 
306
 
        return $data;
307
 
        }
308
 
 
309
 
sub name
310
 
        {
311
 
        my $self = shift;
312
 
        my $data = shift;
313
 
        
314
 
        return $self->{'name'} unless $data;
315
 
 
316
 
        unless( _is_name $data)
317
 
                {
318
 
                $self->error('$data is not a valid name');
319
 
                return;
320
 
                }
321
 
 
322
 
        $self->{'name'}   = _format_name $data;
323
 
        $self->{'Z'}      = _get_Z_by_name $data;
324
 
        $self->{'symbol'} = _get_symbol_by_Z($self->Z);
325
 
 
326
 
        return $data;
327
 
        }
328
 
 
329
 
sub symbol
330
 
        {
331
 
        my $self = shift;
332
 
        my $data = shift;
333
 
        
334
 
        return $self->{'symbol'} unless $data;
335
 
 
336
 
        unless( _is_symbol $data )
337
 
                {
338
 
                $self->error('$data is not a valid chemical symbol');
339
 
                return;
340
 
                }
341
 
 
342
 
        $self->{'symbol'} = _format_symbol $data;
343
 
        $self->{'Z'}      = _get_Z_by_symbol $data;
344
 
        $self->{'name'}   = _get_name_by_Z $self->Z;
345
 
 
346
 
        return $data;
347
 
        }
348
 
 
349
 
sub get_symbol
350
 
        {
351
 
        my $thingy = shift;
352
 
 
353
 
        #since we were asked for a name, we'll suppose that we were passed
354
 
        #either a chemical symbol or a Z.
355
 
        return _get_symbol_by_Z($thingy)      if _is_Z $thingy;
356
 
        return _get_symbol_by_name($thingy)   if _is_name $thingy;
357
 
 
358
 
        #maybe it's already a symbol...
359
 
        return _format_symbol $thingy if _is_symbol $thingy;
360
 
 
361
 
        #we were passed something wierd.  pretend we don't know anything.
362
 
        return;
363
 
        }
364
 
 
365
 
sub _get_symbol_by_name
366
 
        {
367
 
        my $name = shift;
368
 
        
369
 
        return unless _is_name $name;
370
 
 
371
 
        $name = _format_name $name;
372
 
 
373
 
        #not much we can do if they don't pass a proper name
374
 
        foreach( keys %names )
375
 
                {
376
 
                next unless $name eq $names{$_};        
377
 
                return $elements{$_}
378
 
                }
379
 
 
380
 
        return;
381
 
        }
382
 
 
383
 
sub _get_symbol_by_Z
384
 
        {
385
 
        my $Z = shift;
386
 
 
387
 
        #just in case we were passed a symbol rather
388
 
        #then a number
389
 
        return unless _is_Z $Z;
390
 
 
391
 
        return $elements{$Z} if defined $elements{$Z};
392
 
 
393
 
        return;
394
 
        }
395
 
 
396
 
sub get_name
397
 
        {
398
 
        my $thingy = shift;
399
 
 
400
 
        #since we were asked for a name, we'll suppose that we were passed
401
 
        #either a chemical symbol or a Z.
402
 
        return _get_name_by_symbol($thingy) if _is_symbol $thingy;
403
 
        return _get_name_by_Z($thingy)      if _is_Z $thingy;
404
 
 
405
 
        #maybe it's already a name
406
 
        return _format_name $thingy if _is_name $thingy;
407
 
 
408
 
        #we were passed something wierd.  pretend we don't know anything.
409
 
        return;
410
 
        }
411
 
 
412
 
 
413
 
sub _get_name_by_symbol
414
 
        {
415
 
        return _get_name_by_Z( _get_Z_by_symbol(shift) );
416
 
        }
417
 
 
418
 
sub _get_name_by_Z
419
 
        {
420
 
        my $Z = shift;
421
 
 
422
 
        return unless _is_Z $Z;
423
 
 
424
 
        #not much we can do if they don't pass a proper number
425
 
        if( defined $names{$Z} )
426
 
                {
427
 
                return $names{$Z};
428
 
                }
429
 
        
430
 
        return;
431
 
        }
432
 
 
433
 
sub get_Z
434
 
        {
435
 
        my $thingy = shift;
436
 
 
437
 
        #since we were asked for a name, we'll suppose that we were passed
438
 
        #either a chemical symbol or a Z.
439
 
        return _get_Z_by_symbol($thingy) if _is_symbol $thingy;
440
 
        return _get_Z_by_name($thingy)   if _is_name $thingy;
441
 
 
442
 
        #maybe it's already a Z
443
 
        return $thingy if _is_Z $thingy;
444
 
 
445
 
        #we were passed something wierd.  pretend we don't know anything.
446
 
        return;
447
 
        }
448
 
 
449
 
sub _get_Z_by_name
450
 
        {
451
 
        my $name = shift;
452
 
        my ($key, $value);
453
 
 
454
 
        while( ($key, $value) = each %names )
455
 
                {
456
 
                #do a case insensitive match
457
 
                if( lc($value) eq lc($name) )
458
 
                        {
459
 
                        return $key;
460
 
                        }
461
 
                }
462
 
 
463
 
        return;
464
 
        }
465
 
 
466
 
sub _get_Z_by_symbol
467
 
        {
468
 
        my $symbol = shift;
469
 
 
470
 
        #ensure that the first letter is upper case and that the
471
 
        #others are lower case.  this way we can accept data from
472
 
        #sources too dumb to know about chemical symbols or proper
473
 
        #cases.  (and they exist.  i've seen them.)
474
 
        $symbol =~ s/^(.)(.*)$/uc($1).lc($2)/e;
475
 
 
476
 
         if( defined $elements{$symbol} )
477
 
                {
478
 
                return $elements{$symbol};
479
 
                }
480
 
 
481
 
        return;
482
 
        }
483
 
 
484
 
########################################################################
485
 
########################################################################
486
 
#
487
 
# the _is_* functions do some minimal data checking to help other
488
 
# functions guess what sort of input they received
489
 
 
490
 
########################################################################
491
 
sub _is_name
492
 
        {
493
 
        my $data = shift;
494
 
 
495
 
        #at least three alphabetic characters
496
 
        return 0 unless $data =~ m/^[a-z][a-z][a-z][a-z]*$/i;
497
 
 
498
 
        $data = _format_name $data;
499
 
 
500
 
        foreach( keys %names )
501
 
                {
502
 
                return 1 if $data eq $names{$_};
503
 
                }
504
 
        
505
 
        return 0;
506
 
        }
507
 
 
508
 
########################################################################
509
 
sub _is_symbol
510
 
        {
511
 
        my $data = shift;
512
 
 
513
 
        return 0 unless $data =~ m/^u?[a-z]?[a-z]$/i;
514
 
 
515
 
        $data =~ s/^(.)(.*)/uc($1) . lc($2)/e;
516
 
 
517
 
        return 1 if defined $elements{$data};
518
 
 
519
 
        return 0;
520
 
        }
521
 
 
522
 
########################################################################
523
 
sub _is_Z
524
 
        {
525
 
        my $data = shift;
526
 
 
527
 
        return 0 unless $data =~ m/^1?\d?\d$/;
528
 
        return 1 if $data > 0 and $data <= $maximum_Z;
529
 
        return 0;
530
 
        }
531
 
 
532
 
########################################################################
533
 
# _format_symbol
534
 
#
535
 
# input: a string that is supoosedly a chemical symbol
536
 
# output: the string with the first character in uppercase and the
537
 
#  rest lowercase
538
 
#
539
 
# there is no data checking involved.  this function doens't know
540
 
# and doesn't care if the data are valid.  it just does its thing.
541
 
sub _format_symbol
542
 
        {
543
 
        my $data = shift;
544
 
        
545
 
        $data =~ s/^(.)(.*)/uc($1).lc($2)/e;
546
 
 
547
 
        return $data;
548
 
        }
549
 
 
550
 
########################################################################
551
 
# _format_name
552
 
#
553
 
# input: a string that is supoosedly a chemical element's name
554
 
# output: the string with the first character in uppercase and the
555
 
#  rest lowercase
556
 
#
557
 
# there is no data checking involved.  this function doens't know
558
 
# and doesn't care if the data are valid.  it just does its thing.
559
 
#
560
 
# this looks like _format_symbol, but it logically isn't.  someday
561
 
# it might do something different than _format_symbol
562
 
sub _format_name
563
 
        {
564
 
        my $data = shift;
565
 
        
566
 
        $data =~ s/^(.)(.*)/uc($1).lc($2)/e;
567
 
 
568
 
        return $data;
569
 
        }
570
 
 
571
 
########################################################################
572
 
sub AUTOLOAD
573
 
        {
574
 
        my $self = shift;
575
 
        my $data = shift;
576
 
 
577
 
        return unless ref $self;
578
 
 
579
 
        my $method_name = $AUTOLOAD;
580
 
 
581
 
        $method_name =~ s/.*:://;
582
 
 
583
 
        if( $data )
584
 
                {
585
 
                $self->{$method_name} = $data;
586
 
                }
587
 
        elsif( defined $self->{$method_name} )
588
 
                {
589
 
                return $self->{$method_name};
590
 
                }
591
 
        else
592
 
                {
593
 
                return;
594
 
                }
595
 
        }
596
 
 
597
 
1;
598
 
 
599
 
__END__
600
 
 
601
 
=head1 NAME
602
 
 
603
 
Chemistry::Elements - Perl extension for working with Chemical Elements
604
 
 
605
 
=head1 SYNOPSIS
606
 
 
607
 
  use Chemistry::Elements qw(get_name get_Z get_symbol);
608
 
 
609
 
  # the constructor can use different input
610
 
  $element = new Chemistry::Elements $atomic_number;
611
 
  $element = new Chemistry::Elements $chemical_symbol;
612
 
  $element = new Chemistry::Elements $element_name;
613
 
 
614
 
  # you can make up your own attributes by specifying
615
 
  # a method (which is really AUTOLOAD)
616
 
        $element->molar_mass(22.989) #sets the attribute
617
 
  $MM = $element->molar_mass         #retrieves the value
618
 
 
619
 
=head1 DESCRIPTION
620
 
 
621
 
There are two parts to the module:  the object stuff and the exportable
622
 
functions for use outside of the object stuff.  The exportable
623
 
functions are discussed in EXPORTABLE FUNCTIONS.
624
 
 
625
 
Chemistry::Elements provides an easy, object-oriented way to
626
 
keep track of your chemical data.  Using either the atomic
627
 
number, chemical symbol, or element name you can construct
628
 
an Element object.  Once you have an element object, you can
629
 
associate your data with the object by making up your own
630
 
methods, which the AUTOLOAD function handles.  Since each
631
 
chemist is likely to want to use his or her own data, or
632
 
data for some unforesee-able property, this module does not
633
 
try to be a repository for chemical data.
634
 
 
635
 
The Element object constructor tries to be as flexible as possible -
636
 
pass it an atomic number, chemical symbol, or element name and it
637
 
tries to create the object.
638
 
 
639
 
  # the constructor can use different input
640
 
  $element = new Chemistry::Elements $atomic_number;
641
 
  $element = new Chemistry::Elements $chemical_symbol;
642
 
  $element = new Chemistry::Elements $element_name;
643
 
 
644
 
once you have the object, you can define your own methods simply
645
 
by using them.  Giving the method an argument (others will be
646
 
ignored) creates an attribute with the method's name and
647
 
the argument's value.
648
 
 
649
 
  # you can make up your own attributes by specifying
650
 
  # a method (which is really AUTOLOAD)
651
 
        $element->molar_mass(22.989) #sets the attribute
652
 
  $MM = $element->molar_mass         #retrieves the value
653
 
 
654
 
The atomic number, chemical symbol, and element name can be
655
 
retrieved in the same way.
656
 
 
657
 
   $atomic_number = $element->Z;
658
 
   $name          = $element->name;
659
 
   $symbol        = $element->symbol;
660
 
 
661
 
These methods can also be used to set values, although changing
662
 
any of the three affects the other two.
663
 
 
664
 
   $element       = new Chemistry::Elements('Lead');
665
 
 
666
 
   $atomic_number = $element->Z;    # $atomic_number is 82
667
 
 
668
 
   $element->Z(79);
669
 
 
670
 
   $name          = $element->name; # $name is 'Gold'
671
 
 
672
 
=head2 Instance methods
673
 
 
674
 
=over 4
675
 
 
676
 
=item new( Z | SYMBOL | NAME )
677
 
 
678
 
Create a new instance from either the atomic number, symbol, or
679
 
element name.
680
 
 
681
 
=item Z
682
 
 
683
 
Return the atomic number of the element.
684
 
 
685
 
=item name
686
 
 
687
 
Return the name of the element.
688
 
 
689
 
=item symbol
690
 
 
691
 
Return the symbol of the element.
692
 
 
693
 
=back
694
 
 
695
 
=head2 Exportable functions
696
 
 
697
 
These functions can be exported.  They are not exported by default.
698
 
 
699
 
=over 4
700
 
 
701
 
=item get_symbol()
702
 
 
703
 
This function attempts to return the symbol of the chemical element given
704
 
either the chemical symbol, element name, or atmoic number.  The
705
 
function does its best to interpret inconsistent input data (e.g.
706
 
chemcial symbols of mixed and single case).
707
 
 
708
 
        use Chemistry::Elements qw(get_symbol);
709
 
 
710
 
        $name = get_symbol('Fe');     #$name is 'Fe'
711
 
        $name = get_symbol('fe');     #$name is 'Fe'
712
 
        $name = get_symbol(26);       #$name is 'Fe'
713
 
        $name = get_symbol('Iron');   #$name is 'Fe'
714
 
        $name = get_symbol('iron');   #$name is 'Fe'
715
 
 
716
 
If no symbol can be found, nothing is returned.
717
 
 
718
 
Since this function will return the symbol if it is given a symbol,
719
 
you can use it to test whether a string is a chemical symbol
720
 
(although you have to play some tricks with case since get_symbol
721
 
will try its best despite the case of the input data).
722
 
 
723
 
        if( lc($string) eq lc( get_symbol($string) ) )
724
 
                {
725
 
                #stuff
726
 
                }
727
 
        
728
 
You can modify the symbols (e.g. you work for UCal ;) ) by changing
729
 
the data at the end of this module.
730
 
 
731
 
=item get_name()
732
 
 
733
 
This function attempts to return the name the chemical element given
734
 
either the chemical symbol, element name, or atomic number.  The
735
 
function does its best to interpret inconsistent input data (e.g.
736
 
chemcial symbols of mixed and single case).
737
 
 
738
 
        $name = get_name('Fe');     #$name is 'Iron'
739
 
        $name = get_name('fe');     #$name is 'Iron'
740
 
        $name = get_name(26);       #$name is 'Iron'
741
 
        $name = get_name('Iron');   #$name is 'Iron'
742
 
        $name = get_name('iron');   #$name is 'Iron'
743
 
 
744
 
If there is no Z can be found, nothing is returned.
745
 
 
746
 
Since this function will return the name if it is given a name,
747
 
you can use it to test whether a string is a chemical element name
748
 
(although you have to play some tricks with case since get_name
749
 
will try its best despite the case of the input data).
750
 
 
751
 
        if( lc($string) eq lc( get_name($string) ) )
752
 
                {
753
 
                #stuff
754
 
                }
755
 
 
756
 
You can modify the names (e.g. for different languages) by changing
757
 
the data at the end of this module.
758
 
 
759
 
=item get_Z()
760
 
 
761
 
This function attempts to return the atomic number of the chemical
762
 
element given either the chemical symbol, element name, or atomic
763
 
number.  The function does its best to interpret inconsistent input data
764
 
(e.g. chemcial symbols of mixed and single case).
765
 
 
766
 
        $name = get_Z('Fe');     #$name is 26
767
 
        $name = get_Z('fe');     #$name is 26
768
 
        $name = get_Z(26);       #$name is 26
769
 
        $name = get_Z('Iron');   #$name is 26
770
 
        $name = get_Z('iron');   #$name is 26
771
 
 
772
 
If there is no Z can be found, nothing is returned.
773
 
 
774
 
Since this function will return the Z if it is given a Z,
775
 
you can use it to test whether a string is an atomic number.
776
 
You might want to use the string comparison in case the
777
 
$string is not a number (in which case the comparison
778
 
will be false save for the case when $string is undefined).
779
 
 
780
 
        if( $string eq get_Z($string) )
781
 
                {
782
 
                #stuff
783
 
                }
784
 
 
785
 
=back
786
 
 
787
 
The package constructor automatically finds the largest defined
788
 
atomic number (in case you add your own heavy elements).
789
 
 
790
 
=head2 AUTOLOADing methods
791
 
 
792
 
You can pseudo-define additional methods to associate data with objects.
793
 
For instance, if you wanted to add a molar mass attribute, you
794
 
simply pretend that there is a molar_mass method:
795
 
 
796
 
        $element->molar_mass($MM); #add molar mass datum in $MM to object
797
 
 
798
 
Similiarly, you can retrieve previously set values by not specifying
799
 
an argument to your pretend method:
800
 
 
801
 
        $datum = $element->molar_mass();
802
 
 
803
 
        #or without the parentheses
804
 
        $datum = $element->molar_mass;
805
 
 
806
 
If a value has not been associated with the pretend method and the
807
 
object, the pretend method returns nothing.
808
 
 
809
 
I had thought about providing basic data for the elements, but
810
 
thought that anyone using this module would probably have their
811
 
own data.  If there is an interest in canned data, perhaps I can
812
 
provide mine :)
813
 
 
814
 
=head1 TO DO
815
 
 
816
 
I would like make this module easily localizable so that one could
817
 
specify other names or symbols for the elements (i.e. a different
818
 
language or a different perspective on the heavy elements).  If
819
 
anyone should make changes to the data, i would like to get a copy
820
 
so that i can include it in future releases :)
821
 
 
822
 
=head1 COPYRIGHT
823
 
 
824
 
Copright 2005, brian d foy
825
 
 
826
 
You can use this module under the same terms as Perl itself.
827
 
 
828
 
=head1 AUTHOR
829
 
 
830
 
brian d foy, CC< <bdfoy@cpan.org> >>
831
 
 
832
 
=cut