P0w3rK!d has asked for the wisdom of the Perl Monks concerning the following question:

$string = "test@foo.com"; # populated by DB query

How do I replace test@foo.com with test/@foo.com before using with mail?

(This is assuming that the @ will cause a problem as a variable value similar
to the problem it causes in regular perl code)

Replies are listed 'Best First'.
Re: Search and replace for @
by japhy (Canon) on Jul 16, 2001 at 19:46 UTC
    Your worries are ill-founded. And to the people that replied to this post, about interpolation and what-not, re-read his comment: the string is populated from a database query.

    Had he written $x = "foo@bar", he'd have been warned by Perl. But that's not the case.

    In addition, because the @ is in a string, there's no fear of it being read by Perl as an array, unless he evaluates the variable. All in all, he doesn't need to do jack to the string. In fact, adding a backslash will probably cause a mailing client to break.

    _____________________________________________________
    Jeff japhy Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Search and replace for @
by MZSanford (Curate) on Jul 16, 2001 at 19:26 UTC
    I think :
    $string =~ s/@/\\@/g;
    But, i dont think perl will have a problem with $string because it is only a problem during string declaration.
    OH, a sarcasm detector, that’s really useful
      Also, when you populate $string, make sure you use single quotes so @foo is not interpreted as an array variable
      $string = 'foo@foo.com'; $string =~ s/\@/\\@/g;
      That's a very important point; unless you explicitly type into your code the email address, you don't need to escape the @ symbol. So populating $string from a database would need no extra work (and trying to do what you do may screw up the system later by introducing an unneeded backslash).
      Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Search and replace for @
by suaveant (Parson) on Jul 16, 2001 at 19:30 UTC
    In an interpolated string, if you put \Q and \E around some text, especially a variable, it will escape all non word characters with a \ (which is the proper escape character, not /) so something like
    "|/usr/bin/mail \Q$string\E"

    if you want a / you'll have to do something like $string =~ s|\@|/\@|g;

                    - Ant

(jeffa) Re: Search and replace for @
by jeffa (Bishop) on Jul 16, 2001 at 19:28 UTC
    s/@/\/@/g;
    But the real problem lies in how you declare $string, that is, the @ sign must be escaped if you use double quotes:
    $string = "foo\@bar.com"; # or better yet: $string = 'foo@bar.com';
    UPDATE:
    _NOW_ i see the comment that says populated by DB query, sheesh!

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: Search and replace for @
by agoth (Chaplain) on Jul 16, 2001 at 19:31 UTC
    Why not do some research?

    Search this site for 'replacing', there are loads of hits.
    Look in any perl books, they will have the answers....

      Thank you for your tactful RTFM reply.
      ...
      I did search the site. I know how to do this normally. I spcifically could not figure out the combination for the "@" character.

      Also, a search for "replace" on the site yielded nothing.

      A search for "search and replace" did not yield my answer.
      :)
        Fair enough, the post gave me the impression you hadn't bothered, sometimes varying the search to i.e replacing helps