/usr/share/perl5/Data/JavaScript/Anon.pm is in libdata-javascript-anon-perl 1.03-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 | package Data::JavaScript::Anon;
# This package provides a mechanism to convert the main basic Perl
# structures into JavaScript structures, making it easier to transfer
# data from Perl to JavaScript.
use 5.006;
use strict;
use warnings;
use Params::Util qw{ _STRING _SCALAR0 _ARRAY0 _HASH0 };
use Class::Default ();
use vars qw{@ISA $VERSION $errstr $RE_NUMERIC $RE_NUMERIC_HASHKEY %KEYWORD};
BEGIN {
$VERSION = '1.03';
@ISA = 'Class::Default';
$errstr = '';
# Attempt to define a single, all encompasing,
# regex for detecting a legal JavaScript number.
# We do not support the exotic values, such as Infinite and NaN.
my $_sci = qr/[eE](?:\+|\-)?\d+/; # The scientific notation exponent ( e.g. 'e+12' )
my $_dec = qr/\.\d+/; # The decimal section ( e.g. '.0212' )
my $_int = qr/(?:[1-9]\d*|0)/; # The integers section ( e.g. '2312' )
my $real = qr/(?:$_int(?:$_dec)?|$_dec)(?:$_sci)?/; # Merge the integer, decimal and scientific parts
my $_hex = qr/0[xX][0-9a-fA-F]+/; # Hexidecimal notation
my $_oct = qr/0[0-7]+/; # Octal notation
# The final combination of all posibilities for a straight number
# The string to match must have no extra characters
$RE_NUMERIC = qr/^(?:\+|\-)??(?:$real|$_hex|$_oct)\z/;
# The numeric for of the hash key is similar, but without the + or - allowed
$RE_NUMERIC_HASHKEY = qr/^(?:$real|$_hex|$_oct)\z/;
%KEYWORD = map { $_ => 1 } qw{
abstract boolean break byte case catch char class const
continue debugger default delete do double else enum export
extends false final finally float for function goto if
implements import in instanceof int interface long native new
null package private protected public return short static super
switch synchronized this throw throws transient true try typeof
var void volatile while with
};
}
#####################################################################
# Top Level Dumping Methods
sub new {
my $proto = shift;
my $class = ref $proto || $proto;
my $opts = _HASH0($_[0]) ? shift : { @_ };
# Create the object
my $self = bless {
quote_char => '"',
}, $class;
## change the default quote character
if ( defined $opts->{quote_char} && length $opts->{quote_char} ) {
$self->{quote_char} = $opts->{quote_char};
}
return $self;
}
sub _create_default_object {
my $class = shift;
my $self = $class->new();
return $self;
}
sub anon_dump {
my $class = shift;
my $something = shift;
my $processed = shift || {};
# Handle the undefined case
return 'undefined' unless defined $something;
# Handle the basic non-reference case
return $class->anon_scalar( $something ) unless ref $something;
# Check to see if we have processed this reference before.
# This should catch circular, cross-linked, or otherwise complex things
# that we can't handle.
if ( $processed->{$something} ) {
return $class->_err_found_twice( $something );
} else {
$processed->{$something} = 1;
}
# Handle the SCALAR reference case, which in our case we treat
# like a normal scalar.
if ( _SCALAR0($something) ) {
return $class->anon_scalar( $something );
}
# Handle the array case by generating an anonymous array
if ( _ARRAY0($something) ) {
# Create and return the array
my $list = join ', ', map { $class->anon_dump($_, $processed) } @$something;
return "[ $list ]";
}
# Handle the hash case by generating an anonymous object/hash
if ( _HASH0($something) ) {
# Create and return the anonymous hash
my $pairs = join ', ', map {
$class->anon_hash_key($_)
. ': '
. $class->anon_dump( $something->{$_}, $processed )
} keys %$something;
return "{ $pairs }";
}
$class->_err_not_supported( $something );
}
# Same thing, but creating a variable
sub var_dump {
my $class = shift;
my $name = shift or return undef;
my $value = $class->anon_dump( shift );
"var $name = $value;";
}
# Wrap some JavaScript in a HTML script tag
sub script_wrap {
"<script language=\"JavaScript\" type=\"text/JavaScript\">\n$_[1]\n</script>";
}
# Is a particular string a legal JavaScript number.
# Returns true if a legal JavaScript number.
# Returns false otherwise.
sub is_a_number {
my $class = shift;
my $number = (defined $_[0] and ! ref $_[0]) ? shift : '';
$number =~ m/$RE_NUMERIC/ ? 1 : '';
}
#####################################################################
# Basic Variable Creation Statements
# Create a JavaScript scalar given the javascript variable name
# and a reference to the scalar.
sub var_scalar {
my $class = shift;
my $name = shift or return undef;
my $scalar_ref = _SCALAR0(shift) or return undef;
my $value = $class->js_value( $$scalar_ref ) or return undef;
"var $name = $value;";
}
# Create a JavaScript array given the javascript array name
# and a reference to the array.
sub var_array {
my $class = shift;
my $name = shift or return undef;
my $array_ref = _ARRAY0(shift) or return undef;
my $list = join ', ', map { $class->anon_dump($_) } @$array_ref;
"var $name = new Array( $list );";
}
# Create a JavaScript hash ( which is just an object ), given
# the variable name, and a reference to a hash.
sub var_hash {
my $class = shift;
my $name = shift or return undef;
my $hash_ref = _HASH0(shift) or return undef;
my $struct = $class->anon_hash( $name, $hash_ref ) or return undef;
"var $name = $struct;";
}
#####################################################################
# Basic Serialisation And Escaping Methods
# Turn a single perl value into a single javascript value
sub anon_scalar {
my $class = shift;
my $value = _SCALAR0($_[0]) ? ${shift()} : shift;
return 'null' unless defined $value;
# Don't quote if it is numeric
return $value if $value =~ /$RE_NUMERIC/;
my $quote_char = $class->_self->{quote_char};
# Escape and quote
$quote_char . $class->_escape($value) . $quote_char;
}
# Turn a single perl value into a javascript hash key
sub anon_hash_key {
my $class = shift;
my $value = defined($_[0]) && !ref($_[0]) ? shift : return undef;
my $quote_char = $class->_self->{quote_char};
# Quote if it's a keyword
return $quote_char . $value . $quote_char if $KEYWORD{$value};
# Don't quote if it is just a set of word characters or numeric
return $value if $value =~ /^[^\W\d]\w*\z/;
return $value if $value =~ /$RE_NUMERIC_HASHKEY/;
# Escape and quote
$quote_char . $class->_escape($value) . $quote_char;
}
# Create a JavaScript array given the javascript array name
# and a reference to the array.
sub anon_array {
my $class = shift;
my $name = shift or return undef;
my $array_ref = _ARRAY0(shift) or return undef;
my $list = join ', ', map { $class->anon_scalar($_) } @$array_ref;
"[ $list ]";
}
# Create a JavaScript hash ( which is just an object ), given
# the variable name, and a reference to a hash.
sub anon_hash {
my $class = shift;
my $name = shift or return undef;
my $hash_ref = _HASH0(shift) or return undef;
my $pairs = join ', ', map {
$class->anon_hash_key( $_ )
. ': '
. $class->anon_scalar( $hash_ref->{$_} )
} keys %$hash_ref;
"{ $pairs }";
}
#####################################################################
# Utility and Error Methods
sub _escape {
my $class = shift;
my $text = shift;
my $char = $class->_self->{quote_char};
$text =~ s/(\Q$char\E|\\)/\\$1/g; # Escape quotes and backslashes
$text =~ s/\n/\\n/g; # Escape newlines in a readable way
$text =~ s/\r/\\r/g; # Escape CRs in a readable way
$text =~ s/\t/\\t/g; # Escape tabs in a readable way
$text =~ s/([\x00-\x1F])/sprintf("\\%03o", ord($1))/ge; # Escape other control chars as octal
$text;
}
sub _err_found_twice {
my $class = shift;
my $something = ref $_[0] || 'a reference';
$errstr = "Found $something in your dump more than once. "
. "Data::JavaScript::Anon does not support complex, "
. "circular, or cross-linked data structures";
undef;
}
sub _err_not_supported {
my $class = shift;
my $something = ref $_[0] || 'A reference of unknown type';
$errstr = "$something was found in the dump struct. "
. "Data::JavaScript::Anon only supports objects based on, "
. "or references to SCALAR, ARRAY and HASH type variables.";
undef;
}
1;
__END__
=pod
=head1 NAME
Data::JavaScript::Anon - Dump big dumb Perl structs to anonymous JavaScript structs
=head1 SYNOPSIS
# Dump an arbitrary structure to javascript
Data::JavaScript::Anon->anon_dump( [ 'a', 'b', { a => 1, b => 2 } ] );
=head1 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.
=over 4
=item 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.
=item No Circular References
Since circular references can't be defined in a single anonymous struct,
they are not allowed. Try something like L<Data::JavaScript> instead.
Although not supported, they will be detected, and an error returned.
=back
=head1 MAIN METHODS
All methods are called as methods directly, in the form
C<< Data::JavaScript::Anon->anon_dump( [ 'etc' ] ) >>.
=head2 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 C<undef>
if an error is found.
=head2 var_dump $name, STRUCT
As above, but the C<var_dump> method allows you to specify a variable name,
with the resulting JavaScript being C<var name = struct;>. Note that the
method WILL put the trailing semi-colon on the string.
=head2 script_wrap $javascript
The C<script_wrap> method is a quick way of wrapping a normal JavaScript html
tag around your JavaScript.
=head2 is_a_number $scalar
When generating the javascript, numbers will be printed directly and not
quoted. The C<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.
=head2 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 '"'.
=head1 SECONDARY METHODS
The following are a little less general, but may be of some use.
=head2 var_scalar $name, \$scalar
Creates a named variable from a scalar reference.
=head2 var_array $name, \@array
Creates a named variable from an array reference.
=head2 var_hash $name, \%hash
Creates a named variable from a hash reference.
=head2 anon_scalar \$scalar
Creates an anonymous JavaScript value from a scalar reference.
=head2 anon_array \@array
Creates an anonymous JavaScript array from an array reference.
=head2 anon_hash \%hash
Creates an anonymous JavaScript object from a hash reference.
=head2 anon_hash_key $value
Applys the formatting for a key in a JavaScript object
=head1 SUPPORT
Bugs should be reported via the CPAN bug tracker at:
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-JavaScript-Anon>
For other comments or queries, contact the author.
=head1 AUTHOR
Adam Kennedy E<lt>adamk@cpan.orgE<gt>
=head1 SEE ALSO
L<JSON>, L<http://ali.as/>
=head1 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.
=cut
|