Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

still confused with CGI and carriage returns carriage returns

by redpaw (Novice)
on Apr 26, 2000 at 05:53 UTC ( [id://9152]=perlquestion: print w/replies, xml ) Need Help??

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

ok heres the snippit of code and i'm bodging this together from a couple of different sources so bear with me here...
<BEGIN..>
use CGI;
my $req = new CGI;
my $file = $req->param("FILE1");
my $login = $req->param("login");
my $description = $req->param("description");
$description =~ tr/\n/ /;
<END>
everythign is coming in from a CGI post form the problem I'm still having is that when I write the description variable out to a database file it still has the X0D (carriage return) character in it in any place that I have hit the return in the multi line text field I have placed "$description =~ tr/\n/ /;" in the script as someone had pointed out to me before in another post and it had gotten rid of the problem for me in another script but this one I'm working with now has a bit different way of handling the input from the CGI post. any help would be greatly appreciated
  • Comment on still confused with CGI and carriage returns carriage returns

Replies are listed 'Best First'.
Re: still confused with CGI and carriage returns carriage returns
by httptech (Chaplain) on Apr 26, 2000 at 07:24 UTC
    You also may be experiencing ^M characters from Windows clients; so really you should use s/\n|\r/ /g;
Re: still confused with CGI and carriage returns carriage returns
by comatose (Monk) on Apr 26, 2000 at 17:29 UTC

    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.

Re: still confused with CGI and carriage returns carriage returns
by guice (Scribe) on Apr 26, 2000 at 08:10 UTC
    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!

RE: still confused with CGI and carriage returns carriage returns
by Keighvin (Novice) on Apr 26, 2000 at 08:27 UTC
    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!
      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.
Re: still confused with CGI and carriage returns carriage returns
by perlmonkey (Hermit) on Apr 26, 2000 at 06:07 UTC
    (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.
      It works for me:
      my $string = "hi, \n\nhow are you\n?"; $string =~ tr/\n/X/; print ">>$string<<\n";
      Result: >>hi, XXhow are youX?<<
        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 03:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found