in reply to Re^10: Converting hash into js object properties
in thread Converting hash into js object properties

nysus:

OK, I was going on under the working assumption that you were creating JSON stuff that would be used that your JavaScript stuff would fetch from the server and use. It seems like you're actually generating JavaScript in your perl code to send off to the browser and execute. That's fine, of course, but it certainly complicates things a bit. Now you can't easily use JSON to build your data structures and handle the quoting for you because you've also got some plain JavaScript that you don't want to mangle.

I've not worked in that space yet, so you may want to browse CPAN for other modules that might be useful. A perfunctory look gave up JavaScript::Code and JavaScript::Writer, perhaps one of those might be more in line with what you need.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

  • Comment on Re^11: Converting hash into js object properties

Replies are listed 'Best First'.
Re^12: Converting hash into js object properties
by nysus (Parson) on Nov 22, 2017 at 05:17 UTC

    OK, after some trial and error, I came up with this:

    sub hash_walk { my ($hash, $string, $level, %keys_printed) = @_; while (my ($k, $v) = each %$hash) { $string .= "{ $k: " unless $keys_printed{$level}{$k}; $keys_printed{$level}{$k} = 1; if (ref($v) eq 'HASH') { $string = hash_walk($v, $string, ++$level, %keys_printed); $string .= ' }, '; $level--; } else { $string .= $v . ' },'; } } return $string; } my %data = ( email =>{steve => 'owens'}, user => {steve => {fun => 'times', laugh => 'in'}}, pass => {blah => 'moomoo'}, ); print hash_walk(\%data, '', 0, ()) . "\n";

    Now I just need to come up with some code to properly handle the quoting of the values.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      nysus:

      That looks like a good start. I find myself writing variations of that from time to time.

      For checking when you need to quote values, I suggest looking at Scalar::Utils--it provides the function looks_like_number() which I use all the time to do things like that:

      $val = wrap($val, '"') if defined $val and length($val) and ! looks_li +ke_number($val);

      You may also want to handle the array case, as well as protect yourself in the event that your keys aren't legal keys in javascript (i.e., have blanks, punctuation, etc.).

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

      Now I just need to come up with some code to properly handle the quoting of the values.

      In no way am i actually admitting that ive been known to steal code from Data::Dumper to do things like that, but that would be perl quoting

        Heh, I thought about doing that. Didn't want to steal the opportunity from myself to agonize over my goofy hack. :) I'll look now to see how a pro would do it.

        $PM = "Perl Monk's";
        $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
        $nysus = $PM . ' ' . $MCF;
        Click here if you love Perl Monks

Re^12: Converting hash into js object properties
by nysus (Parson) on Nov 22, 2017 at 01:07 UTC