| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Any::Renderer::JavaScript; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | # $Id: JavaScript.pm,v 1.8 2006/08/21 08:30:23 johna Exp $ | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 1 |  |  | 1 |  | 4 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 32 |  | 
| 6 | 1 |  |  | 1 |  | 5 | use vars qw($VERSION %Formats); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 64 |  | 
| 7 | 1 |  |  | 1 |  | 474 | use Data::JavaScript; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | $VERSION = sprintf"%d.%03d", q$Revision: 1.8 $ =~ /: (\d+)\.(\d+)/; | 
| 10 |  |  |  |  |  |  | %Formats = map {$_ => 1} @{available_formats()}; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub new | 
| 13 |  |  |  |  |  |  | { | 
| 14 |  |  |  |  |  |  | my ( $class, $format, $options ) = @_; | 
| 15 |  |  |  |  |  |  | die("The format '$format' isn't supported") unless($Formats{$format}); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | my $self = { | 
| 18 |  |  |  |  |  |  | 'options' => $options, | 
| 19 |  |  |  |  |  |  | }; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | bless $self, $class; | 
| 22 |  |  |  |  |  |  | return $self; | 
| 23 |  |  |  |  |  |  | } | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | sub render | 
| 26 |  |  |  |  |  |  | { | 
| 27 |  |  |  |  |  |  | my ( $self, $data ) = @_; | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | TRACE ( "Rendering w/Data::JavaScript" ); | 
| 30 |  |  |  |  |  |  | DUMP ( $data ); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | my $variable_name = $self->{ 'options' }->{ 'VariableName' } || 'script_output'; | 
| 33 |  |  |  |  |  |  | return Data::JavaScript::jsdump ( $variable_name, $data ); | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | sub requires_template | 
| 37 |  |  |  |  |  |  | { | 
| 38 |  |  |  |  |  |  | return 0; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub available_formats | 
| 42 |  |  |  |  |  |  | { | 
| 43 |  |  |  |  |  |  | return [ 'JavaScript', 'Javascript' ]; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub TRACE {} | 
| 47 |  |  |  |  |  |  | sub DUMP {} | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | 1; | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | =head1 NAME | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | Any::Renderer::JavaScript - render as a JavaScript object | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | use Any::Renderer; | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | my %options = ( 'VariableName' => 'myvariable' ); | 
| 60 |  |  |  |  |  |  | my $format = "JavaScript"; | 
| 61 |  |  |  |  |  |  | my $r = new Any::Renderer ( $format, \%options ); | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | my $data_structure = [...]; # arbitrary structure code | 
| 64 |  |  |  |  |  |  | my $string = $r->render ( $data_structure ); | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | You can get a list of all formats that this module handles using the following syntax: | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | my $list_ref = Any::Renderer::JavaScript::available_formats (); | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | Also, determine whether or not a format requires a template with requires_template: | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | my $bool = Any::Renderer::JavaScript::requires_template ( $format ); | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | Any::Renderer::JavaScript renders any Perl data structure passed to it as a | 
| 77 |  |  |  |  |  |  | sequence of JavaScript statements to create the corresponding data structure in JavaScript. For example: | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | perl -MAny::Renderer -e "print Any::Renderer->new('JavaScript')->render({a => 1, b => [2,3]})" | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | results in: | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | var script_output = new Object;script_output['a'] = 1;script_output['b'] = new Array;script_output['b'][0] = 2;script_output['b'][1] = 3; | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | =head1 FORMATS | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | =over 4 | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =item JavaScript (aka Javascript) | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | =back | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | =head1 METHODS | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | =over 4 | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | =item $r = new Any::Renderer::JavaScript($format,\%options) | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | See L for a description of valid values for C<$format>. | 
| 100 |  |  |  |  |  |  | See L for a description of valid C<%options>. | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | =item $scalar = $r->render($data_structure) | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | The main method. | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | =item $bool = Any::Renderer::JavaScript::requires_template($format) | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | False in this case. | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =item $list_ref = Any::Renderer::JavaScript::available_formats() | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | See L for a list. | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | =back | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | =head1 OPTIONS | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | =over 4 | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | =item VariableName | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | Name of the javascript variable that the new data structure is to be assigned to.  Defaults to C. | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | =back | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | L, L | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | =head1 VERSION | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | $Revision: 1.8 $ on $Date: 2006/08/21 08:30:23 $ by $Author: johna $ | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | =head1 AUTHOR | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | Matt Wilson | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | =head1 COPYRIGHT | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | (c) BBC 2006. This program is free software; you can redistribute it and/or modify it under the GNU GPL. | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | =cut |