I’ve been searching for the best way to normalize an argument list to a string, such that two argument lists convert to the same string iff they are equivalent. My ideal algorithm would
- Compare embedded hashes and lists deeply, rather than by reference
- Ignore hash key order
- Ignore difference between 3 and “3″
- Generate a relatively readable string
- Perform well (XS preferred over Perl)
This is necessary for memoizing a function, or for caching a web page with query arguments.
As a strawman example, Memoize uses this as a default normalizer, which fails #1 and #3:
$argstr = join chr(28),@_;
The best candidate I’ve found to date is
as it is fast, readable, and hash-key-order agnostic. CHI uses this to generate keys from arbitrary references.
JSON::XS treats the number 3 and the string “3″ differently, based on how the scalar was used recently. This can generate different strings for essentially equivalent argument lists and reduce the memoization effect. (The vast majority of functions won’t know or care if they get 3 or “3″.)
For fun I looked at a bunch of serializers to see which ones differentiate 3 and “3″:
Data::Dump : equal -  vs  Data::Dumper : not equal -  vs ['3'] FreezeThaw : equal - FrT;@1|@1|$1|3 vs FrT;@1|@1|$1|3 JSON::PP : not equal -  vs ["3"] JSON::XS : not equal -  vs ["3"] Storable : not equal - <unprintable> YAML : equal - ---n- 3n vs ---n- 3n YAML::Syck : equal - --- n- 3n vs --- n- 3n YAML::XS : not equal - ---n- 3n vs ---n- '3'n
It seems in general like the more sophisticated modules make this differentiation, perhaps because it is more “correct”, though it is the opposite of what I want in this case. Of the ones that report “equal”, not sure how to get them to ignore hash key order.
I could walk the argument list beforehand and stringify all numbers, but this would require making a deep copy and would violate #5.
If I find a great result that requires more than a few lines of code, I’ll stick it in CPAN, e.g.