NAME

Data::JavaScript::LiteObject - lightweight data dumping to JavaScript

SYNOPSIS

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)]);

DESCRIPTION

This module was inspired by Data::JavaScript, 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.

Required parameters

protoName

The name to be used for the prototype object function.

dataRef

A reference to an array of hashes(loh) or hash of hashes(hoh) to dump.

Optional parameters

attributes

A reference to an array containing a list of the object attributes (hash keys). This is useful if every object is not guaranteed to posses a value for each attribute. It could also be used to exclude data from being dumped.

explode

A scalar, if true output is one attribute per line. The default; false; is one object per line.

lineIN

A scalar, if true output is numbered every 5 lines. The value provided should be the number of lines printed before this output. For example if a CGI script included:

    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

A reference to a scalar. jsodump will set the scalar's value to the number of the last line of numbered output produced when lineIN is specified. Thus you may pass the scalar to a subsequent call to jsodump as the value of lineIn for continuous numbering. For example:

jsodump(protoName  => "sandwich",
          dataRef  => \@lunch,
          lineIN   => 4,
          lineOUT  => \$.);
jsodump(protoName  => "sandwich",
          dataRef  => \%lunch,
          lineIN   => $.);
listObjects

A scalar, if true the parameters value is used as the name of an array to be output which will contain a list of all the dumped object. This allows data-ignorant client side code which need only traverse the named array.

jsodump(protoName  => "sandwich",
        dataRef    => \@lunch,
        listObjects=> "sandwiches");

would append the following to the output

sandwiches = new Array('BLT', 'PBnJ');

BUGS

Nothing that am I aware of.

SEE ALSO

Data::JavaScript, Data::Dumper

AUTHOR

Jerrad Pierce jpierce@cpan.org, webmaster@pthbb.org. http://pthbb.org/