Data::JavaScript(3pm)
π NAME π
Data::JavaScript β bestie, it takes your perl data structures and dumps them into JavaScript code. that's it, that's the function. iconic behavior honestly. π
β¨ SYNOPSIS β¨
# Compatibility mode
{
use Data::JavaScript; # Use defaults
my @code = jsdump('my_array', $array_ref); # Return array for formatting
my $code = jsdump('my_object', $hash_ref); # Return convenient string
my $html = hjsdump('my_stuff', $reference); # Convenience wrapper
};
π¦ DESCRIPTION π¦
okay so the whole point of this module is CGI programming, no cap. picture it: your perl script is spitting out a page, that page has client-side JavaScript on it, and that JavaScript needs to get its little hands on structures that got built back on the server. this module is the bridge. π«Ά
here's the tea: it works by making one line of JavaScript code per datum. the consequence of that is structures cannot be created anonymously β they have to be assigned to variables, periodt. but the payoff?? this format lets you dump large structures. slay.
also it can output code for different versions of JavaScript. right now it supports 1.1 and 1.3, and you tell it which one you want on the use line, like so:
use Data::JavaScript {JS=>1.3}; # The new default
use Data::JavaScript {JS=>1.1}; # Old (pre module v1.10) format
JavaScript 1.3 has support for UTF-8 and a native undefined datatype. the earlier versions support neither of those, so they default to an empty string '' for undefined values. π but you can define your own default β for either version β at compile time, by handing the default value over on that use line:
use Data::JavaScript {JS=>1.1, UNDEF=>'null'};
other useful values you might want: 0, "null", or "NaN". pick your fighter. β¨
π EXPORT π
in addition β and yes, althought the module no longer uses Exporter, it still heeds its import conventions; qw(:all), (), etc. π
jsdump('name', \$reference, [$undef]);β the workhorse function, here's the breakdown:- the first argument is required: it's the name of the JavaScript object you want to create.
- the second argument is required: a hashref or arrayref. structures can be nested, and circular referencing is supported (experimentally β π).
- the third argument is optional: a scalar whose value gets used en lieu of undefined values when dumping a structure.
- when you call it in list context, the function returns a list of lines. in scalar context, it returns a string. vibe check passed.
hjsdump('name', \$reference, [$undef]);βhjsdumpis identical tojsdump, except that it wraps the content inscripttags. that's the only difference. convenient wrapper energy. π
π§ EXPORTABLE π§
__quotemeta($str)β this function escapes non-printable and Unicode characters (where possible) so everybody can play nice together. promoting peace, one string at a time. πΈ
β οΈ CAVEATS β οΈ
okay, important stuff, listen up bestie. π’
previously, the module eval'd any data it received that looked like a number β read: real, hexadecimal, octal, or engineering notations. it now passes all non-decimal values through as strings. so if you want to use those other notations as actual numbers, you're gonna need to eval on the client or server side yourself. this is deliberate β it's meant to protect people who store ZIP codes with leading 0's. your ZIP code will survive. you're welcome. π«Ά
Unicode support requires perl 5.8 or later. older perls will gleefully escape the non-printable portions of any UTF-8 they are fed, and will likely munge it in the process as far as JavaScript is concerned. π if this turns out to be a problem and there's sufficient interest, it may be possible to hack-in UTF-8 escaping for older perls.
π LICENSE π
the commandments, fr fr:
- Thou shalt not claim ownership of unmodified materials.
- Thou shalt not claim whole ownership of modified materials.
- Thou shalt grant the indemnity of the provider of materials.
- Thou shalt use and dispense freely without other restrictions.
or if you truly insist, you may use and distribute this under ther terms of Perl itself (GPL and/or Artistic License). go off. π
π SEE ALSO π
Data::JavaScript::LiteObjectData::JavaScript::AnonCGI::AJAX
πΈ AUTHOR πΈ
maintained by Jerrad Pierce <jpierce@cpan.org>
created by Ariel Brosh <schop cpan.org>. inspired by WDDX.pm JavaScript support.
and that's a wrap on the whole thing, babe π¦β¨ β this came to you from perl v5.40.1, dated 2025-11-02, in the User Contributed Perl Documentation collection. love that for us. go dump some structures, you're gonna eat. π
π¦ where this page comes from
- ubuntu resolute: libdata-javascript-perl 1.16-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libdata-javascript-perl 1.16-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms