title: Parsing JavaScript using Perl: getting "undef" from JE->parse
body: <p>I have Javascript code and I need to parse javascript variables into perl hash. Is there any ready-to-use module? I tried <a href="https://metacpan.org/pod/JE#$j-%3Eparse(-$code,-$filename,-$first_line_no-)" rel="nofollow noreferrer">JE::parse()</a> and <a href="https://metacpan.org/pod/JavaScript::HashRef::Decode" rel="nofollow noreferrer">JavaScript::HashRef::Decode</a> but neither worked.</p>
  
  <p>Expected behaviour:</p>
  
  <pre><code>use Data::Dumper;
  use SomeModule::ParseJSVariables qw/decode_js/;
  
  my $str = qq/
  var data = {
      'abc': 1,
      'def' : 2
      'xyz' : { 'foo' : 'bar' }
  }
  /;
  
  my $res = decode_js($str);
  warn Dumper $res; #
  
  # expected result: 
  # { 
  #   name =&gt; 'data', 
  #   value =&gt; {
  #     'abc' =&gt; 1,
  #     'def' =&gt; 2
  #     'xyz' =&gt; { 'foo' =&gt; 'bar' }
  #     }
  # }
  
  
  use JE;
  my $j = new JE;
  my $parsed = $j-&gt;parse($str);
  warn Dumper $parsed; # undef :(
  </code></pre>
  
  <p>If there are no ready-to-use modules, I will be glad if someone suggest right regexp or parsing approach.</p>
  
  <p>UPD, clarification. I have ~ thousand lines of javascript code and I need only get content of variables which are explicitly given in global scope, like var x = { 'foo' : 'bar' }. Other code can be skipped from parsing.</p>
  
  <p>My environment :</p>
  
  <pre><code>$ perl --version
  
  This is perl 5, version 22, subversion 1 (v5.22.1) built for x86_64-linux-gnu-thread-multi
  
  $ lsb_release -a
  No LSB modules are available.
  Distributor ID: Ubuntu
  Description:    Ubuntu 16.04.5 LTS
  Release:    16.04
  Codename:   xenial
  
  $ uname -r
  4.19.24-041924-generic
  </code></pre>