1NAME
2 Data::JavaScript::Anon - Dump big dumb Perl structs to anonymous
3 JavaScript structs
4
5SYNOPSIS
6 # Dump an arbitrary structure to javascript
7 Data::JavaScript::Anon->anon_dump( [ 'a', 'b', { a => 1, b => 2 } ] );
8
9DESCRIPTION
10 Data::JavaScript::Anon provides the ability to dump large simple data
11 structures to JavaScript. That is, things that don't need to be a class,
12 or have special methods or whatever.
13
14 The method it uses is to write anonymous variables, in the same way you
15 would in Perl. The following shows some examples.
16
17 # Perl anonymous array
18 [ 1, 'a', 'Foo Bar' ]
19
20 # JavaScript equivalent ( yes, it's exactly the same )
21 [ 1, 'a', 'Foo Bar' ]
22
23 # Perl anonymous hash
24 { foo => 1, bar => 'bar' }
25
26 # JavaScript equivalent
27 { foo: 1, bar: 'bar' }
28
29 One advantage of doing it in this method is that you do not have to
30 co-ordinate variable names between your HTML templates and Perl. You
31 could use a simple Template Toolkit phrase like the following to get
32 data into your HTML templates.
33
34 var javascript_data = [% data %];
35
36 In this way, it doesn't matter WHAT the HTML template calls a particular
37 variables, the data dumps just the same. This could help you keep the
38 work of JavaScript and Perl programmers ( assuming you were using
39 different people ) seperate, without creating cross-dependencies between
40 their code, such as variable names.
41
42 The variables you dump can also be of arbitrary depth and complexity,
43 with a few limitations.
44
45 ARRAY and HASH only
46 Since arrays and hashs are all that is supported by JavaScript, they
47 are the only things you can use in your structs. Any references or a
48 different underlying type will be detected and an error returned.
49
50 Note that Data::JavaScript::Anon will use the UNDERLYING type of the
51 data. This means that the blessed classes or objects will be ignored
52 and their data based on the object's underlying implementation type.
53
54 This can be a positive thing, as you can put objects for which you
55 expect a certain dump structure into the data to dump, and it will
56 convert to unblessed, more stupid, JavaScript objects cleanly.
57
58 No Circular References
59 Since circular references can't be defined in a single anonymous
60 struct, they are not allowed. Try something like Data::JavaScript
61 instead. Although not supported, they will be detected, and an error
62 returned.
63
64MAIN METHODS
65 All methods are called as methods directly, in the form
66 "Data::JavaScript::Anon->anon_dump( [ 'etc' ] )".
67
68 anon_dump STRUCT
69 The main method of the class, anon_dump takes a single arbitrary data
70 struct, and converts it into an anonymous JavaScript struct.
71
72 If needed, the argument can even be a normal text string, although it
73 wouldn't do a lot to it. :)
74
75 Returns a string containing the JavaScript struct on success, or "undef"
76 if an error is found.
77
78 var_dump $name, STRUCT
79 As above, but the "var_dump" method allows you to specify a variable
80 name, with the resulting JavaScript being "var name = struct;". Note
81 that the method WILL put the trailing semi-colon on the string.
82
83 script_wrap $javascript
84 The "script_wrap" method is a quick way of wrapping a normal JavaScript
85 html tag around your JavaScript.
86
87 is_a_number $scalar
88 When generating the javascript, numbers will be printed directly and not
89 quoted. The "is_a_number" method provides convenient access to the test
90 that is used to see if something is a number. The test handles just
91 about everything legal in JavaScript, with the one exception of the
92 exotics, such as Infinite, -Infinit and NaN.
93
94 Returns true is a scalar is numeric, or false otherwise.
95
96 You may also access method in using an instantiated object.
97
98 new HASH
99 This will create a Data::JavaScript::Anon object that will allow you to
100 change some of the default behaviors of some methods.
101
102 Options:
103 quote_char : Set the quote_char for stirng scalars. Default is '"'.
104
105SECONDARY METHODS
106 The following are a little less general, but may be of some use.
107
108 var_scalar $name, \$scalar
109 Creates a named variable from a scalar reference.
110
111 var_array $name, \@array
112 Creates a named variable from an array reference.
113
114 var_hash $name, \%hash
115 Creates a named variable from a hash reference.
116
117 anon_scalar \$scalar
118 Creates an anonymous JavaScript value from a scalar reference.
119
120 anon_array \@array
121 Creates an anonymous JavaScript array from an array reference.
122
123 anon_hash \%hash
124 Creates an anonymous JavaScript object from a hash reference.
125
126 anon_hash_key $value
127 Applys the formatting for a key in a JavaScript object
128
129SUPPORT
130 Bugs should be reported via the CPAN bug tracker at:
131
132 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-JavaScript-Anon>
133
134 For other comments or queries, contact the author.
135
136AUTHOR
137 Adam Kennedy <adamk@cpan.org>
138
139SEE ALSO
140 JSON, <http://ali.as/>
141
142COPYRIGHT
143 Copyright 2003 - 2009 Adam Kennedy.
144
145 This program is free software; you can redistribute it and/or modify it
146 under the same terms as Perl itself.
147
148 The full text of the license can be found in the LICENSE file included
149 with this module.
150
151