blib/lib/Tie/Scalar/MarginOfError.pm

filter: Statementstmt 100% 23 / 23 Branchbran 100% 4 / 4 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 100% 39 / 39 CC 4 SCAR 7.9
CC
4
Cov
100%
STORE
4.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.2
↑ Summary
1 package Tie::Scalar::MarginOfError;
2  
3 =head1 NAME
4  
5 Tie::Scalar::MarginOfError - Scalars that have margins of error
6  
7 =head1 SYNOPSIS
8  
9 use Tie::Scalar::MarginOfError;
10  
11 tie my $val, 'Tie::Scalar::MarginOfError',
12 {
13 tolerance => 0.1,
14 initial_value => 1,
15 callback => \&some_sub,
16 };
17  
18  
19 =head1 DESCRIPTION
20  
21 This allows you to have a scalar which has to stay within a certain
22 margin of error. Your code will die (or execute what was passed in via
23 the 'callback' subref) if the scalar's value goes outside this range.
24  
25 You tie a variable, and give it an initial value and a tolerance. Your
26 code will die (or execute what was given in the callback subref) if the
27 value gets beyond +/- whatever you have set the tolerance to be.
28  
29 In the SYNOPSIS example, $val will cause your code to execute &some_sub
30 if it gets above 1.1 or below 0.9.
31  
32 If no callback is defined, then the code will simply croak.
33  
34 =head2 More on the callback
35  
36 If you do define a callback, then it will receive one argument, which is
37 the Tie::Scalar::MarginOfError object. This means you can get out the
38 initial value, if you wish to reset the variable once it exceeds the
39 margin of error.
40  
41 See t/Tie-Scalar-MarginOfError.t for that very example.
42  
43 =cut
44  
45 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 1 our $VERSION = "0.03";
49  
50 1 ▶use Tie::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use base 'Tie::StdScalar';
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 15 ▶sub STORE {
Subroutine STORE: called CC=4 SCAR=13.9
56 15 my ($self, $val) = @_;
57 15 ▶ if (($val > $$self->{initial_value} + $$self->{tolerance})
Branch: 1 branch
BranchTrueFalseTotal
if ($val > $$self->{"initial_value"} + $$self->{"tolerance"} or $val < $$self->{"initial_value"} - $$self->{"tolerance"}) 3 12 15
Condition100%3/3
$val > $$self->{"initial_value"} + $$self->{"tolerance"} or $val < $$self->{"initial_value"} - $$self->{"tolerance"}
l!l&&r!l&&!r
1212
MC/DC100%2/2
$val > $$self->{"initial_value"} + $$self->{"tolerance"} or $val < $$self->{"initial_value"} - $$self->{"tolerance"}
$val > $$self->{"initial_value"} + $$self->{"tolerance"} $val < $$self->{"initial_value"} - $$self->{"tolerance"}
Truth table100%3/3
A or B
A= $val > $$self->{"initial_value"} + $$self->{"tolerance"}B= $val < $$self->{"initial_value"} - $$self->{"tolerance"}
ABresult
1X1
011
000
58 || ($val < $$self->{initial_value} - $$self->{tolerance})) {
59 3 ▶ croak "$val is outside margin of error" unless my $subref = $$self->{callback};
Branch: 1 branch
BranchTrueFalseTotal
unless my $subref = $$self->{"callback"} 2 1 3
60 1 $subref->($self);
61 }
62 12 $$self->{value} = $val;
63 }
64  
65 13 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
66 13 my $self = shift;
67 13 return $$self->{value};
68 }
69  
70 =head1 CAVEATS
71  
72 Yes, you could use this to monitor the core temperature of your nuclear
73 reactor. But the variable is tied, so it can be considered slower than
74 normal. And if you are depending on the reactor not going critical, I
75 wouldn't be using this code. Or perl, come to think of it.
76  
77 =head1 HAIKU
78  
79 Want to stay within
80 The limit set by the world
81 Breathe in this module
82  
83 This arose as Tony (http://www.tmtm.com/nothing/) suggested to me that
84 if I can't write the documentation of a module in haiku, then it is
85 doing too many things. As I (also) believe that modules should be
86 responsible for one concept, and one only.
87  
88 Also, I have no poetical ability, so forgive my clumsy attempt.
89  
90 =head1 SEE ALSO
91  
92 perldoc perltie
93  
94 =head1 THANKS
95  
96 o Dave Cross, whose talk to Belfast.pm made me write this. Blame him.
97  
98 o Geert Jan Bex for the subref idea.
99  
100 o Steve Rushe for looking it over and being my personal ispell.
101  
102 =head1 BUGS
103  
104 Let me know if you spot one. Or if your core goes critical and wipes out
105 the Mid-West of the US. But I guess I would see that on the news.
106  
107 =head1 AUTHOR
108  
109 Stray Toaster, E<lt>coder@stray-toaster.co.ukE<gt>
110  
111 =head1 SHOWING YOUR APPRECIATION
112  
113 There was a thread on london.pm mailing list about working in a vacumn -
114 that it was a bit depressing to keep writing modules but never get any
115 feedback. So, if you use and like this module then please send me an
116 email and make my day.
117  
118 All it takes is a few little bytes.
119  
120 (Leon wrote that, not me!)
121  
122 =head1 COPYRIGHT AND LICENSE
123  
124 Copyright (C) 2003 by Stray Toaster
125  
126 This library is free software; you can redistribute it and/or modify
127 it under the same terms as Perl itself, either Perl version 5.8.1 or,
128 at your option, any later version of Perl 5 you may have available.
129  
130  
131 =cut
↑ Summary