Data::JavaScript(3pm)

πŸ“¦
version 1.16 Β· 4 versions
, in ubuntu resolute, ubuntu stonking

πŸŽ€ 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]); β€” hjsdump is identical to jsdump, except that it wraps the content in script tags. 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::LiteObject
  • Data::JavaScript::Anon
  • CGI::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