File Coverage

blib/lib/WDDX/Number.pm
Criterion Covered Total %
statement 6 51 11.7
branch 0 16 0.0
condition 0 15 0.0
subroutine 2 15 13.3
pod n/a
total 8 97 8.2


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -w
2             #
3             # $Id: Number.pm,v 1.3 2003/10/28 17:18:10 andy Exp $
4             #
5             # This code is copyright 1999-2000 by Scott Guelich
6             # and is distributed according to the same conditions as Perl itself
7             # Please visit http://www.scripted.com/wddx/ for more information
8             #
9              
10             package WDDX::Number;
11              
12             # Auto-inserted by build scripts
13             $VERSION = "1.01";
14              
15 1     1   1958 use strict;
  1         2  
  1         39  
16 1     1   25 use Carp;
  1         2  
  1         1037  
17              
18             require WDDX;
19              
20             { my $i_hate_the_w_flag_sometimes = [
21             $WDDX::PACKET_HEADER,
22             $WDDX::PACKET_FOOTER,
23             $WDDX::Number::VERSION
24             ] }
25              
26             1;
27              
28              
29             #/-----------------------------------------------------------------------
30             # Public Methods
31             #
32              
33             sub new {
34 0     0     my( $class, $value ) = @_;
35            
36 0 0         croak "You must supply a value when creating a new $class object\n"
37             unless defined $value;
38            
39 0           $value += 0;
40            
41 0 0 0       if ( $value > 1.7e308 or $value < -1.7e308 ) {
42 0           die "Number exceeds supported range of +/-1.7e308\n";
43             }
44             # Is there a better/more accurate way to handle this?
45             # Also, does it make sense to only restrict precision to after decimal?
46 0 0 0       if ( ($value =~ /^(\+|-)?(\d*)(\.\d+)?(?:E(\+|-)?(\d+))?$/i)
      0        
47             and (defined $3)
48             and (length $3 > 16) ) {
49 0           warn "Floating point number exceeds supported accuracy; " .
50             "trimming to 15 digits.\n";
51 0           $value = ( "$1$2" . substr( $3, 15 ) . "$4$5" ) + 0;
52             }
53            
54 0           my $self = {
55             value => $value,
56             };
57            
58 0           bless $self, $class;
59 0           return $self;
60             }
61              
62              
63             sub type {
64 0     0     return "number";
65             }
66              
67              
68             sub as_packet {
69 0     0     my( $self ) = @_;
70 0           my $output = $WDDX::PACKET_HEADER .
71             $self->_serialize .
72             $WDDX::PACKET_FOOTER;
73             }
74              
75              
76             sub as_scalar {
77 0     0     my( $self ) = @_;
78 0           return $self->_deserialize;
79             }
80              
81              
82             sub as_javascript {
83 0     0     my( $self, $js_var ) = @_;
84 0           return "$js_var=$self->{value};";
85             }
86              
87              
88             #/-----------------------------------------------------------------------
89             # Private Methods
90             #
91              
92             sub is_parser {
93 0     0     return 0;
94             }
95              
96              
97             sub _serialize {
98 0     0     my( $self ) = @_;
99 0           my $val = $self->{value} + 0;
100 0           my $output = "$val";
101            
102 0           return $output;
103             }
104              
105              
106             sub _deserialize {
107 0     0     my( $self ) = @_;
108 0           return $self->{value};
109             }
110              
111              
112             #/-----------------------------------------------------------------------
113             # Parsing Code
114             #
115              
116             package WDDX::Number::Parser;
117              
118              
119             sub new {
120 0     0     return bless { value => "" }, shift;
121             }
122              
123              
124             sub start_tag {
125 0     0     my( $self, $element, $attribs ) = @_;
126            
127 0 0         unless ( $element eq "number" ) {
128 0           die "<$element> not allowed within element\n";
129             }
130            
131 0           return $self;
132             }
133              
134              
135             sub end_tag {
136 0     0     my( $self, $element ) = @_;
137 0           my $value = $self->{value};
138            
139 0 0         unless ( $element eq "number" ) {
140 0           die " not allowed within element\n";
141             }
142            
143 0 0         unless ( $value =~ /^(?:\+|-)?\d*(\.\d+)?(?:E(?:\+|-)?(\d+))?$/i ) {
144 0           die "Invalid numeric value: '$value'\n";
145             }
146 0 0 0       if ( (defined $1) && (length $1 > 16) ) {
147 0           die "Floating point number exceeds supported accuracy (15 digits)\n";
148             }
149 0 0 0       if ( $value > 1.7e308 or $value < -1.7e308 ) {
150 0           die "Number exceeds supported range of +/-1.7e308\n";
151             }
152            
153 0           $self = new WDDX::Number( $value + 0 );
154            
155 0           return $self;
156             }
157              
158              
159             # Not sure if it's appropriate to allow this to be called more than once.
160             # It's a number after all... shouldn't be split by whitespace or other tags.
161             sub append_data {
162 0     0     my( $self, $data ) = @_;
163 0           $self->{value} .= $data;
164             }
165              
166              
167             sub is_parser {
168 0     0     return 1;
169             }
170