Triple escape apostrophes

Discussion in 'Perl Misc' started by dan, Oct 16, 2009.

  1. dan

    dan Guest

    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
    ;
    dan, Oct 16, 2009
    #1
    1. Advertising

  2. dan

    Paul Lalli Guest

    On Oct 16, 9:33 am, dan <> 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
    Paul Lalli, Oct 16, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Huddle

    SQL and apostrophes

    Chris Huddle, Dec 10, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    743
    Steve C. Orr [MVP, MCSD]
    Dec 10, 2003
  2. Andy Fish

    HtmlEncode with apostrophes

    Andy Fish, Apr 6, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    8,650
    Nicole Calinoiu
    Apr 6, 2005
  3. darrel

    URLEncode doesn't like apostrophes?

    darrel, Jul 8, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    506
    darrel
    Jul 8, 2005
  4. Brian van den Broek
    Replies:
    7
    Views:
    757
    Brian van den Broek
    Apr 19, 2005
  5. slomo
    Replies:
    5
    Views:
    1,529
    Duncan Booth
    Dec 2, 2007
Loading...

Share This Page