| 1 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | package Number::Tolerant::Constant 1.711; # TRIAL | ||||||||||
| 4 | # ABSTRACT: a blessed constant type | ||||||||||
| 5 | |||||||||||
| 6 | #pod =head1 SYNOPSIS | ||||||||||
| 7 | #pod | ||||||||||
| 8 | #pod use Number::Tolerant; | ||||||||||
| 9 | #pod use Number::Tolerant::Constant; | ||||||||||
| 10 | #pod | ||||||||||
| 11 | #pod my $range = tolerance(10); | ||||||||||
| 12 | #pod ref $range; # "Number::Tolerant" -- w/o ::Constant, would be undef | ||||||||||
| 13 | #pod | ||||||||||
| 14 | #pod =head1 DESCRIPTION | ||||||||||
| 15 | #pod | ||||||||||
| 16 | #pod When Number::Tolerant is about to return a tolerance with zero variation, it | ||||||||||
| 17 | #pod will return a constant instead. This module will register a constant type that | ||||||||||
| 18 | #pod will catch these constants and return them as Number::Tolerant objects. | ||||||||||
| 19 | #pod | ||||||||||
| 20 | #pod I wrote this module to make it simpler to use tolerances with Class::DBI, which | ||||||||||
| 21 | #pod would otherwise complain that the constructor hadn't returned a blessed object. | ||||||||||
| 22 | #pod | ||||||||||
| 23 | #pod =cut | ||||||||||
| 24 | |||||||||||
| 25 | package | ||||||||||
| 26 | Number::Tolerant::Type::constant_obj; | ||||||||||
| 27 | 1 | ▶ | use parent qw(Number::Tolerant::Type); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | 5 | ▶ | sub construct { shift; | ||||||||
|
Subroutine
construct: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 5 | { value => $_[0], min => $_[0], max => $_[0], constant => 1 } | |||||||||
| 31 | }; | ||||||||||
| 32 | |||||||||||
| 33 | 7 | ▶ | sub parse { | ||||||||
|
Subroutine
parse: called CC=2 SCAR=6.9
| |||||||||||
| 34 | 7 | my ($self, $string, $factory) = @_; | |||||||||
| 35 | 7 | my $number = $self->number_re; | |||||||||
| 36 | 7 | ▶ | return $factory->new($string) if ($string =~ m!\A($number)\z!); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 3 | return; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 1 | ▶ | sub numify { $_[0]->{value} } | ||||||||
|
Subroutine
numify: called CC=1 SCAR=0.0
| |||||||||||
| 41 | |||||||||||
| 42 | 3 | ▶ | sub stringify { "$_[0]->{value}" } | ||||||||
|
Subroutine
stringify: called CC=1 SCAR=0.0
| |||||||||||
| 43 | |||||||||||
| 44 | 9 | ▶ | sub valid_args { | ||||||||
|
Subroutine
valid_args: called CC=3 SCAR=11.0
| |||||||||||
| 45 | 9 | my $self = shift; | |||||||||
| 46 | 9 | my $number = $self->normalize_number($_[0]); | |||||||||
| 47 | |||||||||||
| 48 | 9 | ▶ | return unless defined $number; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | |||||||||||
| 50 | 6 | ▶ | return $number if @_ == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | |||||||||||
| 52 | 1 | return; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | package Number::Tolerant::Constant; | ||||||||||
| 56 | |||||||||||
| 57 | 1 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||
| 58 | 1 | Number::Tolerant->disable_plugin("Number::Tolerant::Type::constant"); | |||||||||
| 59 | 1 | Number::Tolerant->enable_plugin( "Number::Tolerant::Type::constant_obj"); | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 1 | ▶ | sub _disable { | ||||||||
|
Subroutine
_disable: called CC=1 SCAR=0.0
| |||||||||||
| 63 | 1 | Number::Tolerant->disable_plugin("Number::Tolerant::Type::constant_obj"); | |||||||||
| 64 | 1 | Number::Tolerant->enable_plugin( "Number::Tolerant::Type::constant"); | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 1 | 1; | |||||||||
| 68 | |||||||||||
| 69 | __END__ | ||||||||||