File Coverage

blib/lib/Math/Expr/VarSet.pm
Criterion Covered Total %
statement 26 35 74.2
branch 4 10 40.0
condition 8 9 88.8
subroutine 6 7 85.7
pod 4 6 66.6
total 48 67 71.6


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             # VarSet.pm - Represents a set of variables and there values
4             # (c) Copyright 1998 Hakan Ardo
5             #
6             # This program is free software; you can redistribute it and/or modify
7             # it under the terms of the GNU General Public License as published by
8             # the Free Software Foundation; either version 2 of the License, or
9             # any later version.
10             #
11             # This program is distributed in the hope that it will be useful,
12             # but WITHOUT ANY WARRANTY; without even the implied warranty of
13             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14             # GNU General Public License for more details.
15             #
16             # You should have received a copy of the GNU General Public License
17             # along with this program; if not, write to the Free Software
18             # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19              
20             =head1 NAME
21              
22             Math::Expr::VarSet - Represents a set of variables and there values
23              
24             =head1 SYNOPSIS
25              
26             require Math::Expr::VarSet;
27             $s=new Math::Expr::VarSet;
28             $s->Set('a', 7);
29             $s->Get('a');
30              
31             =head1 DESCRIPTION
32              
33             Used to represent variables with values and substitutions.
34              
35             =head1 METHODS
36              
37             =cut
38              
39             package Math::Expr::VarSet;
40 1     1   6 use strict;
  1         2  
  1         7434  
41              
42             =head2 $s=new Math::Expr::VarSet
43              
44             Create a new VarSet object.
45              
46             =cut
47              
48 149     149 0 552 sub new {bless {}, shift}
49              
50             =head2 $s->Set($var, $val)
51              
52             Sets $var to $val. Returns 1 if the $var was already set to $val ore did
53             not have a value previously, otherwise 0.
54              
55             =cut
56              
57             sub Set {
58 95     95 1 173 my ($self, $var, $val) = @_;
59 95         109 my $ok=1;
60              
61             # Parameter sanity checks
62 95 50       240 defined $var || warn "Bad param var: $var";
63 95 50 100     936 $val->isa("Math::Expr::Opp") ||
      66        
64             $val->isa("Math::Expr::Num") ||
65             $val->isa("Math::Expr::Var") || warn "Bad param val: $val";
66              
67 95 100 100     312 if (defined $self->{'Vars'}{$var} &&
  2         2  
68             $self->{'Vars'}{$var}->tostr ne $val->tostr) {$ok=0;}
69 95         240 $self->{'Vars'}{$var}=$val;
70 95         288 $ok;
71             }
72              
73             sub Copy {
74 133     133 0 166 my $self=shift;
75 133         262 my $n=new Math::Expr::VarSet;
76              
77 133         154 foreach (keys %{$self->{'Vars'}}) {
  133         427  
78 11         20 $n->Set($_, $self->{'Vars'}{$_});
79             }
80 133         604 $n;
81             }
82              
83             =head2 $s->Insert($set)
84              
85             Inserts all variables from $set into this object
86              
87             =cut
88              
89             sub Insert {
90 0     0 1 0 my ($self, $set) = @_;
91 0         0 my $ok=1;
92              
93             # Parameter sanity checks
94 0 0       0 $set->isa("Math::Expr::VarSet") || warn "Bad param set: $set\n";
95              
96 0         0 foreach (keys %{$set->{'Vars'}}) {
  0         0  
97 0 0       0 if (defined $self->{'Vars'}{$_}) {$ok=0;}
  0         0  
98 0         0 $self->{'Vars'}{$_}=$set->{'Vars'}{$_};
99             }
100 0         0 $ok;
101             }
102              
103             =head2 $s->tostr
104              
105             Returns a stringrepresentation of the set, usefull for debugging.
106              
107             =cut
108              
109             sub tostr {
110 142     142 1 158 my $self = shift;
111 142         176 my $str="";
112              
113 142         159 foreach (keys %{$self->{'Vars'}}) {
  142         592  
114 24         214 $str .= $_ . "=" . $self->{'Vars'}{$_}->tostr . "\n";
115             }
116              
117 142         507 $str;
118             }
119              
120             =head2 $s->Get($var)
121              
122             Returns the value of $var.
123              
124             =cut
125              
126             sub Get {
127 73     73 1 118 my ($self, $var) = @_;
128              
129 73         228 $self->{'Vars'}{$var};
130             }
131             1;