File: README

package info (click to toggle)
libdata-javascript-anon-perl 1.03-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch, wheezy
  • size: 172 kB
  • ctags: 118
  • sloc: perl: 1,454; makefile: 2
file content (150 lines) | stat: -rw-r--r-- 5,404 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
NAME
    Data::JavaScript::Anon - Dump big dumb Perl structs to anonymous
    JavaScript structs

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

DESCRIPTION
    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.

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

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

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

  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.

  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.

AUTHOR
    Adam Kennedy <adamk@cpan.org>

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

COPYRIGHT
    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.