Subject: | anon_hash_key() bug |
Distribution: Data-JavaScript-Anon-0.2
anon_hash_key() has a very subtle bug in the way it handle quoting of hash keys. Given a hash of Amazon.com ASINS:
my $asins = { '0596000278' => 'Programming Perl' };
While anon_hash_key() rightfully rejects this as a valid octal number, it fails to quote the key because it matches /^\w$/, so given the following code:
use Data::JavaScript::Anon;
print Data::JavaScript::Anon->var_dump('asins', $asins);
...Firefox 0.8 (Linux), Netscape 7.1 (Linux), and Netscape 4.78 (Linux) will all complain thusly:
Warning: 09 is not a legal ECMA-262 octal constant
And they're right for saying so. :-)
The simplest and safest way to handle this is to quote *everything* except strings that are recognized as being numbers. As an aside, there really is no good reason that I can think of for *not* quoting all non-numeric strings...taking advantage of JS's automagical quoting for object key names is just syntactic sugar and doesn't really buy us anything except maybe a few less bytes to download.
173,174c173
< # Don't quote if it is just a set of word characters or numeric
< return $value if $value =~ /^\w+$/;
---
> # Don't quote if it is a numeric string