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

Hi Monks. This is so simple but has been a major pain for me. Here goes:

I read a data source to get a e-mail address. The match is made so the string $emailaddress is populated with (lets say) someone@where.com ... I then call up a mailer and pass the $emailaddress to it so it knows who to send the mail to. My problem: If I manually define:

$emailaddress = 'someone@where.com'; $emailaddress = "someone\@where.com";

The string is passed correctly. I know why I have to escape the @ character or use ticks instead of quotes but how can I escape it AFTER the string is already defined?

The data source record is saved as someone@where.com not someone\@where.com.

Thank you all in advance.
Brandon

Replies are listed 'Best First'.
Re: Simple problem with @ in string.
by almut (Canon) on Feb 06, 2010 at 03:29 UTC
    ...how can I escape it AFTER the string is already defined?

    I could be wrong, but I'd say that @ char is a red herring.  You only need to escape it in literal double quoted strings (as you've shown). After the string literal has been parsed by the Perl interpreter, there is no longer any \ in the value stored in the variable $emailaddress. So why should there be when you're reading the email addresses from some data source (as I understand you're doing)?

    Maybe there's something else in the email addresses, like a trailing newline or some such, that is throwing off the mailer program...  Try debug-printing the values like this "|$emailaddress|", or possibly using Devel::Peek, in order to figure out if there's any junk characters present.

Re: Simple problem with @ in string.
by jethro (Monsignor) on Feb 06, 2010 at 03:34 UTC
    In your second example you have to escape the @ because the string in double quotes gets interpolated. But if you use the variable later, for example with $n= $emailaddress . 'xyz'; or $n= "this is $emailaddress";, no such interpolation takes place in the variable. Naturally in the second example the literal string is interpolated, but not the contents of $emailaddress

    So basically, if your first example works then an unescaped email address read from file will work too

    There are situations where escaping is still necessary, in these cases you can use quotemeta() or a regular expression if only specific characters are to be escaped

      I agree but this is what is happening. Once I read to get the email address I store the email in the string. I write the string in a few other places and its ok. But when I pass the variable like this:

      system("/blat/blat.exe message.txt -t $emailaddress");

      Blat gripes at me because it says it does not like the email address. So I pipe it out to see whats up and if the string contained $emailadress = "me@there.com"; perl is passing it as methere.com.

      Ahhhhhhh .. hold on a second.. Would my system command being called with "" instead of '' cause perl to do that to the string? I guess I need to try that and see?

        Would my system command being called with "" instead of '' cause perl to do that to the string?

        No; that would require Perl 5 to interpolate variables and then interpolate the interpolated values.

        I suspect instead that the error comes from blat.exe or at least system. If you use the list form of system(), you may have better results:

        system( '/blat/blat.exe', 'message.txt', '-t', $emailaddress );

        This avoids any shell interpolation.

        You might also try to substitute 'blat.exe' with 'echo' (if that command exists in the windows shell, someone told me at least msdos had it) to just see what the program gets as parameter. If the @ is still there with echo, then perl and system are not to blame.