GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Data::JavaScript::Anon(3) User Contributed Perl Documentation Data::JavaScript::Anon(3)

Data::JavaScript::Anon - Dump big dumb Perl structs to anonymous JavaScript structs

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

Data::JavaScript::Anon provides the ability to dump large simple data structures to JavaScript. That is, things that don't need to be a class, or have special methods or whatever.

The method it uses is to write anonymous variables, in the same way you would in Perl. The following shows some 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 in this method 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 the following to get data into your HTML templates.

  var javascript_data = [% data %];

In this way, it doesn't matter WHAT the HTML template calls a particular variables, 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, such as variable names.

The variables you dump can also be of arbitrary depth and complexity, with a few limitations.

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.

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.

This can be a positive thing, as 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.

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.

All methods are called as methods directly, in the form "Data::JavaScript::Anon->anon_dump( [ 'etc' ] )".

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.

As above, but the "var_dump" method allows you to 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.

The "script_wrap" method is a quick way of wrapping a normal JavaScript html tag around your JavaScript.

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.

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 '"'.

The following are a little less general, but may be of some use.

Creates a named variable from a scalar reference.

Creates a named variable from an array reference.

Creates a named variable from a hash reference.

Creates an anonymous JavaScript value from a scalar reference.

Creates an anonymous JavaScript array from an array reference.

Creates an anonymous JavaScript object from a hash reference.

Applys the formatting for a key in a JavaScript object

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.

Adam Kennedy <adamk@cpan.org>

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.

2009-04-20 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.