Data::...::Anon(3pm)

πŸ“¦
version 1.03 Β· 2 versions
, in debian bookworm, ubuntu jammy, ubuntu noble

πŸ’– Data::JavaScript::Anon β€” the vibe πŸ’–

okay bestie, welcome to Data::JavaScript::Anon, a User Contributed Perl Documentation page, and honestly? it's giving "dump my big dumb Perl structs straight into anonymous JavaScript structs" and I am obsessed. let's get into it πŸ¦‹


✨ NAME ✨

Data::JavaScript::Anon β€” Dump big dumb Perl structs to anonymous JavaScript structs. that's the whole personality. no cap. πŸ’…


🌸 SYNOPSIS 🌸

# Dump an arbitrary structure to javascript
Data::JavaScript::Anon->anon_dump( [ 'a', 'b', { a => 1, b => 2 } ] );

πŸŽ€ DESCRIPTION πŸŽ€

so here's the tea: Data::JavaScript::Anon gives you the power to dump large simple data structures to JavaScript. like, things that don't need to be a whole class, or have special methods, or whatever. just data. just vibes. ✨

the method it uses is to write anonymous variables, the same way you'd do it in Perl. check these examples:

# Perl anonymous array
[ 1, 'a', 'Foo Bar' ]

# JavaScript equivalent ( yes, it's exactly the same )
[ 1, 'a', 'Foo Bar' ]

# Perl anonymous hash
{ foo => 1, bar => 'bar' }

# JavaScript equivalent
{ foo: 1, bar: 'bar' }

one advantage of doing it this way is that you do not have to co-ordinate variable names between your HTML templates and Perl. you could use a simple Template Toolkit phrase like this to get data into your HTML templates:

var javascript_data = [% data %];

and the way that works? it literally doesn't matter WHAT the HTML template calls a particular variable β€” the data dumps just the same. this could help you keep the work of JavaScript and Perl programmers (assuming you were using different people) seperate, without creating cross-dependencies between their code, like variable names. iconic, honestly. 🫢

the variables you dump can also be of arbitrary depth and complexity β€” with a few limitations. and yes, we're getting into those. 🧚

⚠️ ARRAY and HASH only

since arrays and hashs are all that is supported by JavaScript, they are the only things you can use in your structs. any references or a different underlying type will be detected and an error returned. periodt.

note that Data::JavaScript::Anon will use the UNDERLYING type of the data. this means that the blessed classes or objects will be ignored and their data based on the object's underlying implementation type. πŸ’€

and honestly? this can be a positive thing, because you can put objects for which you expect a certain dump structure into the data to dump, and it will convert to unblessed, more stupid, JavaScript objects cleanly. slay.

🚫 No Circular References

since circular references can't be defined in a single anonymous struct, they are not allowed. try something like Data::JavaScript instead. although not supported, they will be detected, and an error returned. so like, don't try it. πŸ™ˆ


πŸ‘‘ MAIN METHODS πŸ‘‘

all methods are called as methods directly, in the form Data::JavaScript::Anon->anon_dump( [ 'etc' ] ). no constructor required, she's independent like that.

πŸ’œ anon_dump STRUCT

the main method of the class, anon_dump takes a single arbitrary data struct, and converts it into an anonymous JavaScript struct.

if needed, the argument can even be a normal text string, although it wouldn't do a lot to it. :)

returns a string containing the JavaScript struct on success, or undef if an error is found.

πŸ’œ var_dump $name, STRUCT

as above, but the var_dump method lets you specify a variable name, with the resulting JavaScript being var name = struct;. note that the method WILL put the trailing semi-colon on the string. she's thorough. πŸ’…

πŸ’œ script_wrap $javascript

the script_wrap method is a quick way of wrapping a normal JavaScript html tag around your JavaScript. convenient, bestie.

πŸ’œ is_a_number $scalar

when generating the javascript, numbers will be printed directly and not quoted. the is_a_number method provides convenient access to the test that is used to see if something is a number. the test handles just about everything legal in JavaScript, with the one exception of the exotics, such as Infinite, -Infinit and NaN. 😭

returns true is a scalar is numeric, or false otherwise.

you may also access method in using an instantiated object.

πŸ’œ new HASH

this will create a Data::JavaScript::Anon object that will allow you to change some of the default behaviors of some methods.

options:

  • quote_char β€” Set the quote_char for stirng scalars. Default is '"'.

πŸ¦‹ SECONDARY METHODS πŸ¦‹

the following are a little less general, but may be of some use. side characters with main character energy, fr fr.

🌸 var_scalar $name, \$scalar

creates a named variable from a scalar reference.

🌸 var_array $name, \@array

creates a named variable from an array reference.

🌸 var_hash $name, \%hash

creates a named variable from a hash reference.

🌸 anon_scalar \$scalar

creates an anonymous JavaScript value from a scalar reference.

🌸 anon_array \@array

creates an anonymous JavaScript array from an array reference.

🌸 anon_hash \%hash

creates an anonymous JavaScript object from a hash reference.

🌸 anon_hash_key $value

applys the formatting for a key in a JavaScript object.


🫢 SUPPORT 🫢

bugs should be reported via the CPAN bug tracker at:

<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-JavaScript-Anon>

for other comments or queries, contact the author. she's nice, promise. πŸ’–


🌈 AUTHOR 🌈

Adam Kennedy <adamk@cpan.org>


⭐ SEE ALSO ⭐

JSON, <http://ali.as/>


Copyright 2003 - 2009 Adam Kennedy.

this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

the full text of the license can be found in the LICENSE file included with this module.


okay that's the whole page bestie, you ate that πŸ’…βœ¨

perl v5.32.1 β€” dated 2021-12-26, from the Data::...::Anon(3pm) manual. go forth and dump your structs, ily πŸŽ€πŸ¦‹
πŸ“¦ where this page comes from