1JavaScript(3) User Contributed Perl Documentation JavaScript(3)
2
3
4
6 Data::JavaScript - Dump perl data structures into JavaScript code
7
9 use Data::JavaScript; # Use defaults
10
11 @code = jsdump('my_array', $array_ref); # Return array for formatting
12 $code = jsdump('my_object', $hash_ref); # Return convenient string
13 $html = hjsdump('my_stuff', $reference); # Convenience wrapper
14
16 This module is mainly intended for CGI programming, when a perl script
17 generates a page with client side JavaScript code that needs access to
18 structures created on the server.
19
20 It works by creating one line of JavaScript code per datum. Therefore,
21 structures cannot be created anonymously and need to be assigned to
22 variables. However, this format enables dumping large structures.
23
24 The module can output code for different versions of JavaScript. It
25 currently supports 1.1, 1.3 and you specify the version on the "use"
26 line like so:
27
28 use Data::JavaScript {JS=>1.3}; # The new default
29 use Data::JavaScript {JS=>1.1}; # Old (pre module v1.10) format
30
31 JavaScript 1.3 contains support for UTF-8 and a native "undefined"
32 datatype. Earlier versions support neither, and will default to an
33 empty string '' for undefined values. You may define your own
34 default--for either version--at compile time by supplying the default
35 value on the "use" line:
36
37 use Data::JavaScript {JS=>1.1, UNDEF=>'null'};
38
39 Other useful values might be 0, "null", or "NaN".
40
42 In addition, althought the module no longer uses Exporter, it heeds its
43 import conventions; "qw(:all"), "()", etc.
44
45 jsdump('name', \$reference, [$undef]);
46 The first argument is required, the name of JavaScript object to
47 create.
48
49 The second argument is required, a hashref or arrayref. Structures
50 can be nested, circular referrencing is supported (experimentally).
51
52 The third argument is optional, a scalar whose value is to be used
53 en lieu of undefined values when dumping a structure.
54
55 When called in list context, the function returns a list of lines.
56 In scalar context, it returns a string.
57
58 hjsdump('name', \$reference, [$undef]);
59 hjsdump is identical to jsdump except that it wraps the content in
60 script tags.
61
63 __quotemeta($str)
64 This function escapes non-printable and Unicode characters (where
65 possible) to promote playing nice with others.
66
68 Previously, the module eval'd any data it received that looked like a
69 number; read: real, hexadecimal, octal, or engineering notations. It
70 now passes all non-decimal values through as strings. You will need to
71 "eval" on the client or server side if you wish to use other notations
72 as numbers. This is meant to protect people who store ZIP codes with
73 leading 0's.
74
75 Unicode support requires perl 5.8 or later. Older perls will gleefully
76 escape the non-printable portions of any UTF-8 they are fed, likely
77 munging it in the process as far as JavaScript is concerned. If this
78 turns out to be a problem and there is sufficient interest it may be
79 possible to hack-in UTF-8 escaping for older perls.
80
82 · Thou shalt not claim ownership of unmodified materials.
83
84 · Thou shalt not claim whole ownership of modified materials.
85
86 · Thou shalt grant the indemnity of the provider of materials.
87
88 · Thou shalt use and dispense freely without other restrictions.
89
90 Or if you truly insist, you may use and distribute this under ther
91 terms of Perl itself (GPL and/or Artistic License).
92
94 Data::JavaScript::LiteObject, Data::JavaScript::Anon, CGI::AJAX
95
97 Maintained by Jerrad Pierce <jpierce@cpan.org>
98
99 Created by Ariel Brosh <schop cpan.org>. Inspired by WDDX.pm
100 JavaScript support.
101
102
103
104perl v5.32.0 2020-07-28 JavaScript(3)