You also may be experiencing ^M characters from Windows
clients; so really you should use s/\n|\r/ /g;
| [reply] [d/l] |
You should check out this question that was asked
a little more than a week ago: Return?. It's exactly
the same as yours and there are solutions there.
| [reply] |
You may want to use chomp(). chomp() will remove the trailing newlines in any scalar context value it's given.
Since you are trying to log people in via a CGI script you don't have to worry about carrage returns (\r) they are Mac (\r) and Windows (\r\n) only.
The 'uniform' return characture is the newline (\n). All browsers do at least conform to that .. ;)
Doing a chomp($var) will get rid of that pesky newline ...
Read about $/ to see how you can modify chomp() to 'chomp' off what you want from the end of a line.
-- philip
We put the 'K' in kwality! | [reply] |
I've encountered this problem before, but I wasn't using CGI.pm's extensions: I parsed the input myself with some handy bits:
use CGI qw/:standard/;
read(STDIN, $formdata, $ENV{'CONTENT_LENGTH'});
@pairs = split(/\&/, $formdata);
foreach $pair (@pairs){
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%0D%0A/\n/g;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
The above code runs everything through nice and quick, and if you still want to use your CGI.pm extensions you can
(since this doesn't rely on it, you can also omit that line). The beauty of this script is it will read everything in
and parse it at once so you don't have to go fishing for any of the values you need. The values you accessed before
would now be:
$FORM{'FILE1'}, $FORM{'login'}, $FORM{'description'};
Similarly you're free to use a "foreach $key (keys %FORM){};" on this. Enjoy! | [reply] [d/l] [select] |
An addendum to my code snippit: It does *not* remove carriage returns, rather it takes the common extra byte that comes with the \n and changes it into *just* a \n. If you still want to get rid of these use =~ tr/\n//; before you pass it into the %FORM.
| [reply] |
(Okay I wrote then when I must have been on crack, its wrong, so see the replies ...)
Original text:
Your problem is the$description =~ tr/\n/ /;
You should use:
$description =~ s/\n/ /g;
I think tr/// treats the "\n" liternally as a "\" and "n",
while the s/// will treat it as a metacharacter. | [reply] [d/l] [select] |
my $string = "hi, \n\nhow are you\n?";
$string =~ tr/\n/X/;
print ">>$string<<\n";
Result: >>hi, XXhow are youX?<< | [reply] [d/l] |
Okay so I am an ass yet again. I did try it on my system before
I rambled and the tr did not work, so I must have had a dumb little typo.
Just for kicks I benchmarked the s/// vs tr/// since they
seem to do the same in this reguard:
use Benchmark;
$str = "\n#\n#\nSTUFF\n#\n#\n";
timethese(1000000, {
's' => sub { $a = $str; $a=~s/\n/ /g; },
'tr' => sub { $a = $str; $a=~tr/\n/ /; }
});
RESULTS:
Benchmark: timing 1000000 iterations of s, tr...
s: 11 wallclock secs (11.22 usr + 0.00 sys = 11.22 CPU)
tr: 3 wallclock secs ( 3.61 usr + 0.00 sys = 3.61 CPU)
The moral of the story: Use tr/// where you can. | [reply] [d/l] |