How to substitute "@" for "\@" in a string?

J

Jürgen Exner

jbauza said:
That statement does not work. I only worked when I used & # 0 6 4
instead of the @ sign.

I have no idea what statement didn't work because you didn't quote any
context.
But to answer you question in the Subject line:

C:\tmp>type t.pl
use strict; use warnings;
my $var = 'foo@bar';
print "Before: $var\n";
$var =~ s/\@/\\@/;
print "After: $var\n";

C:\tmp>t.pl
Before: foo@bar
After: foo\@bar

jue
 
C

Chris Davies

jbauza said:
Do you have any solution for the real problem? I need to send and
email from a perl script, under red hat linux. What I am using now is
this:
$SendMail = "mail -s ' some text here ' $MailUser";
open( MAIL, "| $SendMail ") or die "$$: Can't run $SendMail: $!";
$MailUser is coming from a web form

You have more than google groups quoting problems.

Consider what happens if $MailUser is, eg, '; rm -f *'.
Chris
 
A

Aaron Baugher

jbauza said:
$SendMail = "mail -s ' some text here' $MailUser";
open( MAIL, "| $SendMail ") or die "$$: Can't run $SendMail: $!";
select MAIL;

$MailUser is read from a web form, I am not setting it.
Please let me know if there is a better way of doing this.

In that case, you may want to consider what will happen if someone
types something like this into that form field:

(e-mail address removed) | rm -rf /

Quoting your @ isn't your biggest problem.
 
J

jbauza

Aaron said:
In that case, you may want to consider what will happen if someone
types something like this into that form field:

(e-mail address removed) | rm -rf /

Quoting your @ isn't your biggest problem.

Thanks for alerting me to the problem.

To answer all previous msgs that were beaten me up for not quoting:
What does not work is any attempt to substitute, split, find the index
of the @ sign in the string. I have to use & # 0 6 4 ; to be able to
find the @ sign.

The problem was solved by using single quotes instead of double quotes
as someone told me to do.

One more comment: Please some of you guys, don't be so aggresive and
cynical...
 
A

A. Sinan Unur

I have to use & # 0 6 4 ; to be able to find the @ sign.

No you don't. You should use CGI.pm or a comparable module to process
form input.

Try the following script at http://www.unur.com/cgi-bin/atsign.pl

#!/usr/bin/perl

use strict;
use warnings;

use CGI;

$| = 1;

run();

sub run {
my $cgi = CGI->new;

if ($cgi->param('text')) {
process_form($cgi);
}
else {
show_form($cgi);
}
}

sub process_form {
my ($cgi) = @_;

my $text = $cgi->param('text');
$text =~ s'@'_there was an at sign here_'g;
print $cgi->header('text/plain'), $text, "\n";;
}

sub show_form {
my ($cgi) = @_;
print $cgi->header('text/html'), <<EO_HTML;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head><title>Form Test</title></head>
<body>
<form action="@{[$cgi->url]}" method="get">
<p><input type="text" name="text"> <input type="submit">
</form>
</body>
</html>
EO_HTML
}
 
J

Jürgen Exner

jbauza said:
To answer all previous msgs that were beaten me up for not quoting:
What does not work is any attempt to substitute, split, find the index
of the @ sign in the string. I have to use & # 0 6 4 ; to be able to
find the @ sign.

The problem was solved by using single quotes instead of double quotes
as someone told me to do.

Well, in _that_ case your obvious problem is that you are not using strict
and warnings.
Otherwise perl would have told you right in your face that you are doing
something that probably is not what you intended:

C:\tmp>type t.pl
use strict; use warnings;
print "foo@bar";
C:\tmp>t.pl
Possible unintended interpolation of @bar in string at C:\tmp\t.pl line 2.
Global symbol "@bar" requires explicit package name at C:\tmp\t.pl line 2.
Execution of C:\tmp\t.pl aborted due to compilation errors.

jue
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top