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

adding double quotes to the jQuery directly didn't break anything

jQuery is "just" a library, in that way it is similar to, for example, Moo: it just makes good/clever use of existing language syntax to provide an interface that makes it look like it's adding new syntax to the language (e.g. jQuery's $(...), Moo's has), but it is in fact working within the confines of the language (other modules can be added to provide sugar). The same way one can write regular Perl subs into Moo classes and use everything that Perl has to offer, one can use all of vanilla JavaScript when working with jQuery.

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

    Thanks.

    I have run into a bit of a roadblock. If a value is set to some jQuery code (let's say to get the value of a text field), it surrounds the jQuery code with quotes which does break things. Can you think of any good ways around this problem?

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

      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.

        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