in reply to Providing perl array data to javascript code

one of the options: create a webpage that check the availability of the file
with some interval
<html> <body onload = 'init()'> <script lang = 'text/javascript'> function init(){ setInterval(function(){ get('numbers.json', function(r){ if(r == 'error'){ console.log('waiting for data'); } for(var i in r['num']){ console.log(r['num'][i]['text'] + ': ' + r['num'][i]['value']); } })}, 5000); } function get(file, callback){ var xmlHttpRequest = new XMLHttpRequest(); xmlHttpRequest.open('GET', '/' + file, true); xmlHttpRequest.setRequestHeader('Content-Type', 'application/x-www-for +m-urlencoded'); xmlHttpRequest.onreadystatechange = function (){ if(xmlHttpRequest.status == 404){ xmlHttpRequest.abort(); callback('error'); return; } if(xmlHttpRequest.status == 200 && xmlHttpRequest.readyState == 4) { callback(JSON.parse(xmlHttpRequest.responseText)); return; } } xmlHttpRequest.send(); } </script> </body> </html>
then perl script which prepares json:
%a = qw/one 1 two 2 three 3 four 4 five 5/; $l = keys %a; open OUT,'>numbers.json' or die $!; print OUT "{\"num\":[\n"; for(sort keys %a){ print OUT '{"text":"'.$_.'", "value":"'.$a{$_}.'"}'; --$l ? print OUT ",\n" : print OUT "\n"; } print OUT "]}\n"; close OUT;
everything goes around and checks the file
if there is none, the thing is ok with it
once the file appears, it loads its innards
ugly ? yes,
working ? also yes
alrighty then