in reply to Re: Perl, JavaScript and quoting/escaping
in thread Perl, JavaScript and quoting/escaping

This is great!  However I have one question.  Is there an alternative to text_to_js_lit packaged as a module?

I'm surprised that (q{\'\'} eq q{\'\'});  wouldn't this be eq q{''}?

In any case the reason I ask is that for testing I'd like to pass perl strings to JS alert().  The problem was at first:

alert("Error: ERROR:  null value in column "bidder_username" violates not-null constraint");  Where I was so lost as to why I couldn't get my string to popup.

Then this:
alert("Error: ERROR:  null value in column "bidder_username" violates not-null constraint");

Where the result is not readable, this string is displayed as is.
  • Comment on Re^2: Perl, JavaScript and quoting/escaping

Replies are listed 'Best First'.
Re^3: Perl, JavaScript and quoting/escaping
by ikegami (Patriarch) on May 06, 2011 at 00:12 UTC

    [ Please don't use <pre> here. Start paragraphs with <p>, and wrap computer text (code, input, output, etc) in <c>...</c>. <c>...</c> will even handle escaping "&", "<", ">", "[" and "]" for you. ]

    Is there an alternative to text_to_js_lit packaged as a module?

    package Module; sub text_to_js_lit { my $s = @_ ? $_[0] : $_; $s =~ s/\\/\\\\/g; $s =~ s/'/\\'/g; # ... return qq{'$s'}; } 1;

    I'm surprised that (q{\'\'} eq q{\'\'}); wouldn't this be eq q{''}?

    Say again?

    Where I was so lost as to why I couldn't get my string to popup. [...] Where the result is not readable, this string is displayed as is.

    What? You just said the same piece of code didn't display the message and displayed it as-is.

      Looking for a CPAN module that's hopefully maintained with a bug tracker. For example there are vary good functions for escaping for URL, HTML, many forms of SQL, ect, ect, ect. Where is the one for JavaScript?
      Haha, lol.
      I'm just not used to the markup of this forum and I can't escape properly.

      qq{\&#39;\&#39;} eq q{&#39;&#39;}

      " VS &quote;

        I'm surprised that (qq{\&#39;\&#39;} eq q{&#39;&#39;}; wouldn't this be eq q{''}?

        Still makes no sense to me.

        " VS &quote;

        So you're asking how to embed double quotes in a JS double-quoted string literal? This isn't really an appropriate place to ask that. One escapes them with a backslash:

        alert("foo \"bar\" baz"); // foo "bar" baz