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