Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Triple escape apostrophes (http://www.velocityreviews.com/forums/t911600-triple-escape-apostrophes.html)

dan 10-16-2009 01:33 PM

Triple escape apostrophes
 
Solution

Having trouble with a javascript alert which contained an apostrophe
within a perl CGI script, through trial and error I eventually found out
that triple escaping the apostrophe works. No idea why.

#!/usr/bin/perl -T
use CGI qw/:standard/;

my $JSCRIPT=<<EOF;
function alertme() {
alert('apostrophe\\\'s');
}
EOF
;

print
header,
start_html( -script => $JSCRIPT ),
start_form(),
submit( -onClick=> "alertme()" ),
end_form,
end_html
;

Paul Lalli 10-16-2009 01:40 PM

Re: Triple escape apostrophes
 
On Oct 16, 9:33*am, dan <spam.meple...@ntlworld.com> wrote:
> Solution
>
> Having trouble with a javascript alert which contained an apostrophe
> within a perl CGI script, through trial and error I eventually found out
> that triple escaping the apostrophe works. No idea why.
>
> #!/usr/bin/perl -T
> use CGI qw/:standard/;
>
> my $JSCRIPT=<<EOF;
> * function alertme() {
> * * alert('apostrophe\\\'s');
> * }
> EOF


When you use a here-doc without any quotes around the here-doc marker,
perl interprets it as being double quoted. So what you wrote is no
different than:

my $JSCRIPT = " function alertme() {\n alert('apostrophe\\\'s');
\n }\n";

Since that string is in double quotes, any backslashes in it need to
be escaped. So your first two "\\" reduce to a single backslash.
Your next "\'" reduce to a single apostrophe. Therefore, what ends up
printed to your browser is

alert('apostrophe\'s')

That single slash is needed by javascript to escape the apostrophe,
since the apostrophe is also the string delimeter.

You could reduce the number of slashes by putting single quotes around
your heredoc marker, so that Perl treats it as a single-quoted string
rather than a double-quoted string

my $JSCRIPT=<<'EOF'
whateverwhatever
EOF

Paul Lalli


All times are GMT. The time now is 05:23 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57