1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#!/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( <STDIN> )
{
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> ";
}
|