So I'm pretty much finished a major chunk of a JavaScript -> Perl translation. I've got a page that shells out to execute the Perl code, embeds the JavaScript code and has the browser display both results for comparison. (This particular component might lend itself to a JavaScript based test framework, but that's because it's an isolated part of the whole task.)
Now that I've fixed the transcription errors and a couple of other bugs that cropped up in translation, I'm finding that the outputs don't quite match. After a lot of sleuthing, I found the offending snippet of code:
for (i = 0; i < keywords.length; $i++) { if (keywords[i].search(input) != -1) { input = escape(input); break; } }The problem here is that the meaning of the test is reversed. The String.search method takes a regex to find, so if the keyword is "fuzzy" and the input is "z", the test matches, so the input is escaped.
The intended meaning is to search through a list of keywords and see if the input is a match. The actual meaning is to see if the input is a keyword or a subset of a keyword. Naturally, I coded the intended meaning in my Perl version, not the actual meaning:
## Is the input a keyword that should be escaped? my %keyword = map {$_ => 1} qw(fuzzy ...); $input = escape($input) if $keyword{$input};Therefore, terms like "z" are unintentionally escaped via JavaScript, but aren't in Perl.
The real stumbling block though was making the mental adjustment to JavaScript. Grabbing a definitive reference was the key here: