blib/lib/Data/JavaScript/Anon.pm

filter: Statementstmt 66.4% 85 / 128 Branchbran 45.0% 27 / 60 Conditioncond 38.8% 7 / 18 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 60.8% 14 / 23 Podpod 100% 12 / 12 totaltotal 58.3% 147 / 252 CC 38 SCAR 20.8
CC
38
Cov
58%
var_scalar
20.0
var_hash
20.0
var_array
12.0
CRAP 8.0
↑ Summary
1 package Data::JavaScript::Anon;
2  
3 # This package provides a mechanism to convert the main basic Perl
4 # structures into JavaScript structures, making it easier to transfer
5 # data from Perl to JavaScript.
6  
7 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Params::Util qw{ _STRING _SCALAR0 _ARRAY0 _HASH0 };
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Class::Default ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use vars qw{@ISA $VERSION $errstr $RE_NUMERIC $RE_NUMERIC_HASHKEY %KEYWORD};
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 $VERSION = '1.03';
16 2 @ISA = 'Class::Default';
17 2 $errstr = '';
18  
19 # Attempt to define a single, all encompasing,
20 # regex for detecting a legal JavaScript number.
21 # We do not support the exotic values, such as Infinite and NaN.
22 2 my $_sci = qr/[eE](?:\+|\-)?\d+/; # The scientific notation exponent ( e.g. 'e+12' )
23 2 my $_dec = qr/\.\d+/; # The decimal section ( e.g. '.0212' )
24 2 my $_int = qr/(?:[1-9]\d*|0)/; # The integers section ( e.g. '2312' )
25 2 my $real = qr/(?:$_int(?:$_dec)?|$_dec)(?:$_sci)?/; # Merge the integer, decimal and scientific parts
26 2 my $_hex = qr/0[xX][0-9a-fA-F]+/; # Hexidecimal notation
27 2 my $_oct = qr/0[0-7]+/; # Octal notation
28  
29 # The final combination of all posibilities for a straight number
30 # The string to match must have no extra characters
31 2 $RE_NUMERIC = qr/^(?:\+|\-)??(?:$real|$_hex|$_oct)\z/;
32  
33 # The numeric for of the hash key is similar, but without the + or - allowed
34 2 $RE_NUMERIC_HASHKEY = qr/^(?:$real|$_hex|$_oct)\z/;
35  
36 2 %KEYWORD = map { $_ => 1 } qw{
37 abstract boolean break byte case catch char class const
38 continue debugger default delete do double else enum export
39 extends false final finally float for function goto if
40 implements import in instanceof int interface long native new
41 null package private protected public return short static super
42 switch synchronized this throw throws transient true try typeof
43 var void volatile while with
44 };
45 }
46  
47  
48  
49  
50  
51 #####################################################################
52 # Top Level Dumping Methods
53  
54 2 ▶sub new {
Subroutine new: called CC=5 SCAR=16.7
Pod documented
55 2 my $proto = shift;
56 2 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
57 2 ▶ my $opts = _HASH0($_[0]) ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
&_HASH0($_[0]) ? : 0 2 2
58  
59 # Create the object
60 2 my $self = bless {
61 quote_char => '"',
62 }, $class;
63  
64 ## change the default quote character
65 2 ▶ if ( defined $opts->{quote_char} && length $opts->{quote_char} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"quote_char"} and length $opts->{"quote_char"}) 1 1 2
Condition66%2/3
defined $opts->{"quote_char"} and length $opts->{"quote_char"}
!ll&&!rl&&r
101
MC/DC50%1/2
defined $opts->{"quote_char"} and length $opts->{"quote_char"}
defined $opts->{"quote_char"} length $opts->{"quote_char"}
Truth table66%2/3
A and B
A= defined $opts->{"quote_char"}B= length $opts->{"quote_char"}
ABresult
0X0
100
111
66 1 $self->{quote_char} = $opts->{quote_char};
67 }
68  
69 2 return $self;
70 }
71  
72 1 ▶sub _create_default_object {
Subroutine _create_default_object: called CC=1 SCAR=0.0
73 1 my $class = shift;
74 1 my $self = $class->new();
75 1 return $self;
76 }
77  
78 19 ▶sub anon_dump {
Subroutine anon_dump: called CC=8 SCAR=21.1
Pod documented
79 19 my $class = shift;
80 19 my $something = shift;
81 19 ▶ my $processed = shift || {};
Condition100%2/2
shift() || {}
l!l
163
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
82  
83 # Handle the undefined case
84 19 ▶ return 'undefined' unless defined $something;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $something 0 19 19
85  
86 # Handle the basic non-reference case
87 19 ▶ return $class->anon_scalar( $something ) unless ref $something;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $something 13 6 19
88  
89 # Check to see if we have processed this reference before.
90 # This should catch circular, cross-linked, or otherwise complex things
91 # that we can't handle.
92 6 ▶ if ( $processed->{$something} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($processed->{$something}) { } 0 6 6
93 0 return $class->_err_found_twice( $something );
94 } else {
95 6 $processed->{$something} = 1;
96 }
97  
98 # Handle the SCALAR reference case, which in our case we treat
99 # like a normal scalar.
100 6 ▶ if ( _SCALAR0($something) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_SCALAR0($something)) 1 5 6
101 1 return $class->anon_scalar( $something );
102 }
103  
104 # Handle the array case by generating an anonymous array
105 5 ▶ if ( _ARRAY0($something) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_ARRAY0($something)) 3 2 5
106 # Create and return the array
107 3 my $list = join ', ', map { $class->anon_dump($_, $processed) } @$something;
108 3 return "[ $list ]";
109 }
110  
111 # Handle the hash case by generating an anonymous object/hash
112 2 ▶ if ( _HASH0($something) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_HASH0($something)) 2 0 2
113 # Create and return the anonymous hash
114 my $pairs = join ', ', map {
115 2 $class->anon_hash_key($_)
116 . ': '
117 2 . $class->anon_dump( $something->{$_}, $processed )
118 } keys %$something;
119 2 return "{ $pairs }";
120 }
121  
122 0 $class->_err_not_supported( $something );
123 }
124  
125 # Same thing, but creating a variable
126 0 ▶sub var_dump {
Subroutine var_dump: not called CC=2 SCAR=17.9
Pod documented
127 0 my $class = shift;
128 0 ▶ my $name = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or return undef 0 0 0
129 0 my $value = $class->anon_dump( shift );
130 0 "var $name = $value;";
131 }
132  
133 # Wrap some JavaScript in a HTML script tag
134 0 ▶sub script_wrap {
Subroutine script_wrap: not called CC=1 SCAR=6.9
Pod documented
135 0 "<script language=\"JavaScript\" type=\"text/JavaScript\">\n$_[1]\n</script>";
136 }
137  
138 # Is a particular string a legal JavaScript number.
139 # Returns true if a legal JavaScript number.
140 # Returns false otherwise.
141 74 ▶sub is_a_number {
Subroutine is_a_number: called CC=4 SCAR=14.9
Pod documented
142 74 my $class = shift;
143 74 ▶ my $number = (defined $_[0] and ! ref $_[0]) ? shift : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && !ref($_[0]) ? : 74 0 74
Condition33%1/3
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
0074
MC/DC0%0/2
defined $_[0] and not ref $_[0]
defined $_[0] not ref $_[0]
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not ref $_[0]
ABresult
0X0
100
111
144 74 ▶ $number =~ m/$RE_NUMERIC/ ? 1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$number =~ /$Data::JavaScript::Anon::RE_NUMERIC/ ? : 60 14 74
145 }
146  
147  
148  
149  
150  
151 #####################################################################
152 # Basic Variable Creation Statements
153  
154 # Create a JavaScript scalar given the javascript variable name
155 # and a reference to the scalar.
156 0 ▶sub var_scalar {
Subroutine var_scalar: not called CC=4 SCAR=30.0
Pod documented
157 0 my $class = shift;
158 0 ▶ my $name = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or return undef 0 0 0
159 0 ▶ my $scalar_ref = _SCALAR0(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $scalar_ref = &_SCALAR0(shift()) or return undef 0 0 0
160 0 ▶ my $value = $class->js_value( $$scalar_ref ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $class->js_value($$scalar_ref) or return undef 0 0 0
161 0 "var $name = $value;";
162 }
163  
164 # Create a JavaScript array given the javascript array name
165 # and a reference to the array.
166 0 ▶sub var_array {
Subroutine var_array: not called CC=3 SCAR=24.8
Pod documented
167 0 my $class = shift;
168 0 ▶ my $name = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or return undef 0 0 0
169 0 ▶ my $array_ref = _ARRAY0(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $array_ref = &_ARRAY0(shift()) or return undef 0 0 0
170 0 my $list = join ', ', map { $class->anon_dump($_) } @$array_ref;
171 0 "var $name = new Array( $list );";
172 }
173  
174 # Create a JavaScript hash ( which is just an object ), given
175 # the variable name, and a reference to a hash.
176 0 ▶sub var_hash {
Subroutine var_hash: not called CC=4 SCAR=30.0
Pod documented
177 0 my $class = shift;
178 0 ▶ my $name = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or return undef 0 0 0
179 0 ▶ my $hash_ref = _HASH0(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $hash_ref = &_HASH0(shift()) or return undef 0 0 0
180 0 ▶ my $struct = $class->anon_hash( $name, $hash_ref ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $struct = $class->anon_hash($name, $hash_ref) or return undef 0 0 0
181 0 "var $name = $struct;";
182 }
183  
184  
185  
186  
187  
188 #####################################################################
189 # Basic Serialisation And Escaping Methods
190  
191 # Turn a single perl value into a single javascript value
192 16 ▶sub anon_scalar {
Subroutine anon_scalar: called CC=4 SCAR=13.9
Pod documented
193 16 my $class = shift;
194 16 ▶ my $value = _SCALAR0($_[0]) ? ${shift()} : shift;
Branch: 1 branch
BranchTrueFalseTotal
&_SCALAR0($_[0]) ? : 1 15 16
195 16 ▶ return 'null' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 16 16
196  
197 # Don't quote if it is numeric
198 16 ▶ return $value if $value =~ /$RE_NUMERIC/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /$Data::JavaScript::Anon::RE_NUMERIC/ 2 14 16
199  
200 14 my $quote_char = $class->_self->{quote_char};
201  
202 # Escape and quote
203 14 $quote_char . $class->_escape($value) . $quote_char;
204 }
205  
206 # Turn a single perl value into a javascript hash key
207 66 ▶sub anon_hash_key {
Subroutine anon_hash_key: called CC=6 SCAR=18.2
Pod documented
208 66 my $class = shift;
209 66 ▶ my $value = defined($_[0]) && !ref($_[0]) ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && !ref($_[0]) ? : 66 0 66
Condition33%1/3
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
0066
MC/DC0%0/2
defined $_[0] and not ref $_[0]
defined $_[0] not ref $_[0]
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not ref $_[0]
ABresult
0X0
100
111
210  
211 66 my $quote_char = $class->_self->{quote_char};
212  
213 # Quote if it's a keyword
214 66 ▶ return $quote_char . $value . $quote_char if $KEYWORD{$value};
Branch: 1 branch
BranchTrueFalseTotal
if $Data::JavaScript::Anon::KEYWORD{$value} 59 7 66
215  
216 # Don't quote if it is just a set of word characters or numeric
217 7 ▶ return $value if $value =~ /^[^\W\d]\w*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^[^\W\d]\w*\z/ 2 5 7
218 5 ▶ return $value if $value =~ /$RE_NUMERIC_HASHKEY/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /$Data::JavaScript::Anon::RE_NUMERIC_HASHKEY/ 1 4 5
219  
220 # Escape and quote
221 4 $quote_char . $class->_escape($value) . $quote_char;
222 }
223  
224 # Create a JavaScript array given the javascript array name
225 # and a reference to the array.
226 0 ▶sub anon_array {
Subroutine anon_array: not called CC=3 SCAR=24.8
Pod documented
227 0 my $class = shift;
228 0 ▶ my $name = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or return undef 0 0 0
229 0 ▶ my $array_ref = _ARRAY0(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $array_ref = &_ARRAY0(shift()) or return undef 0 0 0
230 0 my $list = join ', ', map { $class->anon_scalar($_) } @$array_ref;
231 0 "[ $list ]";
232 }
233  
234 # Create a JavaScript hash ( which is just an object ), given
235 # the variable name, and a reference to a hash.
236 0 ▶sub anon_hash {
Subroutine anon_hash: not called CC=3 SCAR=24.8
Pod documented
237 0 my $class = shift;
238 0 ▶ my $name = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or return undef 0 0 0
239 0 ▶ my $hash_ref = _HASH0(shift) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $hash_ref = &_HASH0(shift()) or return undef 0 0 0
240 my $pairs = join ', ', map {
241 0 $class->anon_hash_key( $_ )
242 . ': '
243 0 . $class->anon_scalar( $hash_ref->{$_} )
244 } keys %$hash_ref;
245 0 "{ $pairs }";
246 }
247  
248  
249  
250  
251  
252 #####################################################################
253 # Utility and Error Methods
254  
255 18 ▶sub _escape {
Subroutine _escape: called CC=1 SCAR=0.0
256 18 my $class = shift;
257 18 my $text = shift;
258 18 my $char = $class->_self->{quote_char};
259 18 $text =~ s/(\Q$char\E|\\)/\\$1/g; # Escape quotes and backslashes
260 18 $text =~ s/\n/\\n/g; # Escape newlines in a readable way
261 18 $text =~ s/\r/\\r/g; # Escape CRs in a readable way
262 18 $text =~ s/\t/\\t/g; # Escape tabs in a readable way
263 18 $text =~ s/([\x00-\x1F])/sprintf("\\%03o", ord($1))/ge; # Escape other control chars as octal
264 18 $text;
265 }
266  
267 0 ▶sub _err_found_twice {
Subroutine _err_found_twice: not called CC=2 SCAR=17.9
268 0 my $class = shift;
269 0 ▶ my $something = ref $_[0] || 'a reference';
Condition0%0/2
ref $_[0] || 'a reference'
l!l
00
MC/DC0%0/1
ref $_[0] || 'a reference'
ref $_[0]
Truth table0%0/2
A
A= ref $_[0]
Aresult
11
00
270 0 $errstr = "Found $something in your dump more than once. "
271 . "Data::JavaScript::Anon does not support complex, "
272 . "circular, or cross-linked data structures";
273 0 undef;
274 }
275  
276 0 ▶sub _err_not_supported {
Subroutine _err_not_supported: not called CC=2 SCAR=17.9
277 0 my $class = shift;
278 0 ▶ my $something = ref $_[0] || 'A reference of unknown type';
Condition0%0/2
ref $_[0] || 'A reference of unknown type'
l!l
00
MC/DC0%0/1
ref $_[0] || 'A reference of unknown type'
ref $_[0]
Truth table0%0/2
A
A= ref $_[0]
Aresult
11
00
279 0 $errstr = "$something was found in the dump struct. "
280 . "Data::JavaScript::Anon only supports objects based on, "
281 . "or references to SCALAR, ARRAY and HASH type variables.";
282 0 undef;
283 }
284  
285 2 1;
286  
287 __END__
↑ Summary