ginocapelli has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am running a classifieds site and am trying to include the caption of the ad in the mailto link. When I do this: href="mailto:me@here.com?subject=$form_data{'caption'}" and there is an "&" in the caption, the subject line ends there. I know that the ampersand is used to concatenate string, but I am wondering if there is a way to use double quotes to interpolate the caption variable?

Replies are listed 'Best First'.
(Ovid) Re: ampersand in
by Ovid (Cardinal) on Sep 08, 2001 at 01:42 UTC

    The following line is not valid Perl:

    href="mailto:me@here.com?subject=$form_data{'caption'}"

    To make it valid, we'd have to do something like this:

    $href="mailto:me\@here.com?subject=$form_data{'caption'}";

    However, your use of %form_data makes me wonder. That's a very commonly used variable when people are hand-parsing CGI data. Such hand parsing is very prone to bugs and this could be the source of your error. Since it's difficult, at best, to debug unseen code, could you post a relevent snippet of the code so we can see what's going on?

    Cheers,
    Ovid

    Vote for paco!

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: ampersand in
by Zaxo (Archbishop) on Sep 08, 2001 at 02:02 UTC

    Here's how to find out where the caption gets truncated. Insert:

    print "<p>$form_data{'caption'}</p>$/"; # DEBUG
    after each block of code. Then you will know which use needs a different form of quoting or escape. If you're parsing cgi on your own, it's likely that & is taken as an URL delimiter and you never see the rest of the caption.

    Ovid hints at this, I'll spell it out:

    use CGI; # or die

    If this tactic doesn't show you what's wrong, please post the code here.

    After Compline,
    Zaxo

      Here is the code (the script is www.e-classifieds.net Standard Edition):
      if (($fields[$index_of_pmail] eq "No" || $fields[$index_of_pmail] eq " +") && ($form_data{'show_temp_ads'} eq "")) { print qq~ <a href="$script_url?session_key=$session_key&display_reply_form_butto +n=on&results_format=off&db_id=$fields[$index_of_db_id]&exact_match=on +&query=retrieval">Reply to Ad</a>~; } else { print qq~ <a href="mailto:$fields[$index_of_email]?subject=Reply To Ad #$fields[ +$index_of_id] at $sitename">$fields[$index_of_email]</a>~; }
      Even if I try to display a link like this: <a href="mailto:rob@ebrae.com?subject=Bob & Tom">rob@ebrae.com</a> The subject line still reads: Subect: "Bob " Do you think there is even a way around this, regardless if the output is cgi generated?
        Try replacing your spaces with the '+' sign...
        <a href="mailto:rob@ebrae.com?subject=Bob+&+Tom">rob@ebrae.com</a>
        More generally you should look at the uri_escape function in the URI::Escape module.

        -Blake