You have to convert the array to a string in Perl, then back to an array in JScript. A while ago, I wrote a serializer I can adapt to this situation. Here it is:
In Perl, change
sub populate {
my @array = (...);
return @array;
}
to
# Serializes an array, a hash or a list which contains only
# strings and undefs. Everything else will be stringified.
sub serialize_string_list {
return join('|',
map {
(defined($_)
? do { local $_=$_; s/\^/^1/g; s/\|/^2/g; $_ }
: '^0'
)
} @_
);
}
sub populate {
my @array = (...);
return serialize_string_list @array;
}
In the emitted JavaScript, change
function do_populate_cb(s) {
... do something with s...
}
to
// Deserializes a list serialized with serialize_string_list.
function deserialize_string_list(s) {
var fields = s.split("|")
var i;
for (i=0; i<fields.length; i++) {
if (fields[i] == "^0") {
fields[i] = null;
} else {
var re;
re = /\^2/g;
fields[i] = fields[i].replace(re, "|");
re = /\^1/g;
fields[i] = fields[i].replace(re, "^");
}
}
return fields;
}
function do_populate_cb(s) {
var array = deserialize_string_list(s);
var i;
for (i=0; i<array.length; i++) {
... do something with array[i]...
}
}
Important: The above only supports lists (and arrays) of strings. Lists of numbers will work, but they will be converted to arrays of strings. List containing references of any sort will not work as expected.
Background: I didn't use a single-character escape mechanism (such as preceeding metacharacters with a slash) since it makes deserialization hard. (i.e. "Should I split on this pipe, or is that an escaped pipe?") The escape mechanism I used -- replacing the seperator character with another character -- avoids that problem, simplifying parsing. IP over Serial Line (SLIP) and maybe Point to Point Protocol (PPP) use a similar escaping algorithm to escape packet delimiters because the delimiters cannot appear inside a packet. |