File Coverage

blib/lib/Thrift/Parser/Type/Number.pm
Criterion Covered Total %
statement 34 35 97.1
branch 12 14 85.7
condition n/a
subroutine 9 9 100.0
pod 3 3 100.0
total 58 61 95.0


line stmt bran cond sub pod time code
1             package Thrift::Parser::Type::Number;
2              
3             =head1 NAME
4              
5             Thrift::Parser::Type::Number - Number base class
6              
7             =head1 DESCRIPTION
8              
9             This class inherits from L. See the docs there for all the usage details.
10              
11             =cut
12              
13 6     6   34 use strict;
  6         11  
  6         201  
14 6     6   31 use warnings;
  6         10  
  6         161  
15 6     6   31 use Scalar::Util qw(blessed);
  6         12  
  6         311  
16 6     6   33 use base qw(Thrift::Parser::Type);
  6         11  
  6         776  
17              
18 6     6   34 use overload '""' => sub { $_[0]->value }, 'eq' => sub { $_[0]->value };
  6     9   14  
  6         110  
  9         3418  
  3         695  
19              
20             =head1 USAGE
21              
22             Firstly, you can use objects in this class in string context; the stringification overload will display the number, as you'd expect.
23              
24             =head2 compose
25              
26             Call with a signed number. Throws L.
27              
28             =cut
29              
30             sub compose {
31 48     48 1 60679 my ($class, $value) = @_;
32              
33 48 50       124 Thrift::Parser::InvalidTypedValue->throw("'undef' is not valid for $class") if ! defined $value;
34              
35 48 100       155 if (blessed $value) {
36 2 100       20 if (! $value->isa($class)) {
37 1         13 Thrift::Parser::InvalidArgument->throw("$class compose() can't take a value of ".ref($value));
38             }
39 1         5 return $value;
40             }
41              
42 46 100       103 if ($class eq 'Thrift::Parser::Type::double') {
43 1 50       9 if ($value !~ m{^-?\d+\.?\d*$}) {
44 0         0 Thrift::Parser::InvalidTypedValue->throw("Value '$value' is not a float");
45             }
46             }
47             else {
48 45 100       283 if ($value !~ m{^-?\d+$}) {
49 6         61 Thrift::Parser::InvalidTypedValue->throw("Value '$value' is not a signed real number");
50             }
51              
52 39         172 my $bit = sprintf '%d', (log($class->_max_value) / log(2)) + 1;
53              
54 39 100       158 Thrift::Parser::InvalidTypedValue->throw("Value '$value' exceeds signed $bit-bit range")
55             if abs($value * 1) > $class->_max_value;
56             }
57              
58 36         175 return $class->SUPER::compose($value);
59             }
60              
61             sub values_equal {
62 4     4 1 65 my ($class, $value_a, $value_b) = @_;
63 4         24 return $value_a == $value_b;
64             }
65              
66             sub value_plain {
67 8     8 1 705 my $self = shift;
68 8         20 return $self->value + 0; # ensure that it's a Perl number
69             }
70              
71             =head1 COPYRIGHT
72              
73             Copyright (c) 2009 Eric Waters and XMission LLC (http://www.xmission.com/). All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
74              
75             The full text of the license can be found in the LICENSE file included with this module.
76              
77             =head1 AUTHOR
78              
79             Eric Waters
80              
81             =cut
82              
83             1;