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