Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Having a problem replacing "@" with "\@"

by TASdvlper (Monk)
on Apr 19, 2005 at 18:36 UTC ( #449377=perlquestion: print w/replies, xml ) Need Help??

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


I have a list of email addresses ( in a configuration file, what I need to do is replace the "@" with "\@". I've tried regexp but couldn't get it to work, so I'm trying this:

$$email[0] = join('\@', split("@",$$email[0]));
Where $$email[0] is the email address. When I do the above, it actually replaced "@" with "\\@", I've tried:
$$email[0] = join("\\\@", split("@",$$email[0]));
and that doesn't work either. It substitutes "@" with "\\\@". This should be something simple to do, I just can't get a handle on it.

Well, thanks all. All solutions worked, and I actually had it working myself, but because I was printing out my hash table of email addresses using "Dumper" it must have automatically substituted "\@" with "\\@", but if I just print out a single hash element, it looks fine. Urgh ! I just didn't realize the output from using Dumper, looks slightly different. Hate when I do stuff like that.

Replies are listed 'Best First'.
Re: Having a problem replacing "@" with "\@"
by Tanktalus (Canon) on Apr 19, 2005 at 19:00 UTC

    I'm really actually quite interested in why you need to do this. Generally speaking, when you load a string from a file, the perl interpreter doesn't get involved, so you end up with a literal @ in the string, and there's no need to escape it.

    That's not to say that this isn't a valid requirement, only that, without information to the contrary, the likelihood of a real desire to escape this character is really quite low. Not zero, but still quite low.

      If you are sending mail with a script, and have something like print(MAIL "To: someone\@somewhere.someorg\n"); the @ needs to be escaped. Of course, one could use single quotes here (except for the newline), but one might want to store the address in a variable and have that and some other things interpolated, and then the @ may cause problems if it's not escaped. It is often possible to get around that with some cleverness, but sometimes it's easier to just escape the troublesome characters.
      (At least, that's what I imagine the OP might be dealing with.)
      (Update: After thinking about it for a while, I can't really think of a convincing reason to escape the @. My brain is just not working well today...please ignore me! I'll shut up for the rest of the day.)
Re: Having a problem replacing "@" with "\@"
by ambs (Pilgrim) on Apr 19, 2005 at 18:39 UTC
    Why don't you use a substitution?
    $$email[0] =~ s!@!\\@!;
    Doesn't this work?

    Alberto Simões

Re: Having a problem replacing "@" with "\@"
by sasikumar (Monk) on Apr 19, 2005 at 18:44 UTC
    Try this
    my $mailaddress=''; $mailaddress=~s/\@/\\\@/;
Re: Having a problem replacing "@" with "\@"
by Transient (Hermit) on Apr 19, 2005 at 18:40 UTC
    #!/usr/bin/perl -w use strict; my $email = ''; # this will replace all @'s - add option of (2) to split to replace on +ly one print join( '\@', split( '\@', $email ) ), "\n"; # this will replace only one - add g to replace all $email =~ s/@/\\@/; print $email, "\n";

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://449377]
Approved by sweetblood
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2022-12-03 05:18 GMT
Find Nodes?
    Voting Booth?

    No recent polls found