|
Data::JavaScript::LiteObject - lightweight data dumping to JavaScript |
Data::JavaScript::LiteObject - lightweight data dumping to JavaScript
use Data::JavaScript:LiteObject;
#OR
use Data::JavaScript:LiteObject '1.2';
%A = (protein => 'bacon',
condiments => 'mayonaise',
produce => [qw(lettuce tomato)]);
%B = (protein => 'peanut butter',
condiments => 'jelly');
@lunch = (\%A, \%B);
%lunch = (BLT=>\%A, PBnJ=>\%B);
jsodump(protoName => "sandwich",
dataRef => \%lunch
attributes => [qw(condiments protein produce)]);
This module was inspired by the Data::JavaScript manpage, which while incredibly versatile, seems rather brute force and inelegant for certain forms of data. Specifically a series of objects of the same class, which it seems is a likely use for this kind of feature. So this module was created to provide a lightweight means of producing configurable, clean and compact output.
LiteObject is used to format and output loh, hoh, lohol, and hohol. The output is JavaScript 1.0 compatible, with the limitation that none of the properties be a single-element array whose value is a number. To lift this limitation pass use the extra value '1.2', which will generate JavaScript 1.2 compatible output.
One function, jsodump, is exported. jsodump accepts a list of named parameters; two of these are required and the rest are optional.
protoNamedataRefhashes(loh) or hash of hashes(hoh) to dump.
attributesexplodelineIN
print q(<html>
<head>
<title>Pthbb!!</title>
<script language=javascript>);>
jsodump(protoName => "sandwich",
dataRef => \@lunch,
lineIN => 4);
The client would see:
<html>
<head>
<title>Pthbb!!</title>
<script language=javascript>
// 5
function sandwich (condiment, produce, protein) {
this.condiment = condiment; this.produce = produce; this.protein = protein; }
BLT = new sandwich('mayonaise', new Array('lettuce','tomato'), 'bacon' );
PBnJ = new sandwich('jelly', '', 'peanut butter' );
// 10
making it easier to read and/or debug.
lineOUT
jsodump(protoName => "sandwich",
dataRef => \@lunch,
lineIN => 4,
lineOUT => \$.);
jsodump(protoName => "sandwich",
dataRef => \%lunch,
lineIN => $.);
listObjects
jsodump(protoName => "sandwich",
dataRef => \@lunch,
listObjects=> "sandwiches");
would append the following to the output
sandwiches = new Array('BLT', 'PBnJ');
Nothing that am I aware of.
the Data::JavaScript manpage, the Data::Dumper manpage
Jerrad Pierce jpierce@cpan.org, webmaster@pthbb.org. http://pthbb.org/
|
Data::JavaScript::LiteObject - lightweight data dumping to JavaScript |