in reply to Accessing javascript variable from perl

That's more a JS question

I think you are only declaring the function without calling it.

  'function(){JSON.stringify(leg);}'

So better try

  '(function(){return JSON.stringify(leg);})()

Or just skip the anonymous function

  'JSON.stringify(leg)'

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Update:

And Corion is of course right, you need - contrary to Perl - an explicit return in side JS functions

Update

added return to code