line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Math::SimpleVariable; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
require 5.005_62; |
4
|
2
|
|
|
2
|
|
9226
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
85
|
|
5
|
2
|
|
|
2
|
|
13
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
61
|
|
6
|
2
|
|
|
2
|
|
19
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
199
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
9
|
|
|
|
|
|
|
use fields ( |
10
|
2
|
|
|
|
|
10
|
'name', # a string with the name of the variable |
11
|
|
|
|
|
|
|
'value', # the numerical value of the variable |
12
|
2
|
|
|
2
|
|
1778
|
); |
|
2
|
|
|
|
|
3314
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
|
|
|
|
|
|
# parse the arguments |
16
|
6
|
|
|
6
|
0
|
27
|
my $proto = shift; |
17
|
6
|
|
33
|
|
|
36
|
my $pkg = ref($proto) || $proto; |
18
|
6
|
|
|
|
|
14
|
my %arg = (); |
19
|
6
|
100
|
66
|
|
|
30
|
if(@_ == 1 && defined(ref $_[0])) { |
20
|
2
|
100
|
|
|
|
29
|
if(ref($_[0])->isa('Math::SimpleVariable')) { |
|
|
50
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# new() was invoked as a copy ctor |
22
|
1
|
|
|
|
|
5
|
return $_[0]->clone(); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
elsif(ref($_[0]) eq 'HASH') { |
25
|
|
|
|
|
|
|
# the arguments are passed in a ref to a hash |
26
|
1
|
|
|
|
|
2
|
%arg = %{$_[0]}; |
|
1
|
|
|
|
|
4
|
|
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
else { |
29
|
0
|
|
|
|
|
0
|
croak "Invalid argument passed to Math::SimpleVariable::new()"; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
else { |
33
|
4
|
|
|
|
|
13
|
%arg = @_; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# build the object |
37
|
5
|
|
|
|
|
18
|
my Math::SimpleVariable $this = fields::new($pkg); |
38
|
5
|
|
|
|
|
4802
|
while(my($k,$v) = each %arg) { |
39
|
8
|
|
|
|
|
33
|
$this->{$k} = $v; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# further initialize the object (e.g. apply default values were no value was given) |
43
|
5
|
|
|
|
|
13
|
$this->initialize(); |
44
|
|
|
|
|
|
|
|
45
|
4
|
|
|
|
|
21
|
return $this; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub initialize { # override this method in derived() classes |
49
|
5
|
|
|
5
|
0
|
8
|
my Math::SimpleVariable $this = shift; |
50
|
5
|
100
|
|
|
|
32
|
defined($this->{name}) or die "No name specified for variable"; |
51
|
4
|
|
|
|
|
7
|
return 1; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub clone { # makes a deep copy of the object |
55
|
|
|
|
|
|
|
# You need to override clone() in derived classes carrying additional data! |
56
|
2
|
|
|
2
|
0
|
172
|
my Math::SimpleVariable $this = shift; |
57
|
2
|
|
|
|
|
12
|
return Math::SimpleVariable->new( |
58
|
|
|
|
|
|
|
name => $this->{name}, |
59
|
|
|
|
|
|
|
value => $this->{value}, |
60
|
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
### Accessors (including useful aliases) |
64
|
|
|
|
|
|
|
sub name { |
65
|
7
|
|
|
7
|
1
|
1123
|
my Math::SimpleVariable $this = shift; |
66
|
7
|
|
|
|
|
29
|
return $this->{name}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub id { |
70
|
1
|
|
|
1
|
1
|
3
|
my Math::SimpleVariable $this = shift; |
71
|
1
|
|
|
|
|
4
|
return $this->name(); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub value { |
75
|
6
|
|
|
6
|
1
|
9
|
my Math::SimpleVariable $this = shift; |
76
|
6
|
|
|
|
|
35
|
return $this->{value}; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub evaluate { |
80
|
1
|
|
|
1
|
1
|
2
|
my Math::SimpleVariable $this = shift; |
81
|
1
|
|
|
|
|
3
|
return $this->value(); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
### I/O |
85
|
|
|
|
|
|
|
sub stringify { |
86
|
1
|
|
|
1
|
1
|
29
|
my Math::SimpleVariable $this = shift; |
87
|
1
|
|
|
|
|
3
|
return $this->name(); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
1; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
__END__ |