Strange behaviour with '\r' character

I

i5513

Hi!. I don't understand ...

At a html page:
<form name="form1" method="POST" action="example.cgi" >
<textarea name="text"></textarea>
</form>

At a cgi script (example.cgi)
(After get parameters)
if ($FORM{"text"} =~ /^(\s|\n|\r)*$/)
{
print "<HTML>";
if ($FORM{"text"} =~ /\r/)
{
print "Hey! It is here! <BR> ";
}
if ($FORM{"texto_in"} =~ /^(\s|\n)*$/)
{
print "Now it isn't here!";
}
print "</HTML>";
exit (1);
}

Well, When I execute (on Windows) this script with one simple return
on textarea. It gives me next result:
Hey! It is here!
Now it isn't here!

What does Perl do with '\r' character?

Thanks you!
 
T

Tad McClellan

i5513 said:
Hi!. I don't understand ...


It would be easier for us to help you if you actually said
what it is that you don't understand...

(After get parameters)


You should use the CGI.pm module for that.

if ($FORM{"text"} =~ /^(\s|\n|\r)*$/)
^^ ^^
^^ ^^ this part will *never* match


These will match exactly the same as the above:

if ($FORM{"text"} =~ /^\s*$/)

if ($FORM{"text"} =~ /^[\r\n\t\f ]*$/)


if ($FORM{"text"} =~ /^(\s|\n)*$/)
{
print "Now it isn't here!";
}


I'll guess that you do not understand why this condition is true?

It is true because \s matches a carriage return.

If you want to check that there is no carriage return, then write
an expression that checks that there is no carriage return:

if ($FORM{"text"} !~ /^\r*$/)
^^
^^ note

or

unless ($FORM{"text"} =~ /^\r*$/)

with one simple return
on textarea.


Saying it in Perl is always better than trying to say it in English.

You say that sentence in Perl like this:

my %FORM = ( text => "\r\n" );
or
$FORM{text} = "\r\n";


Have you seen the Posting Guidelines that are posted here frequently?

It gives me next result:
Hey! It is here!
Now it isn't here!


Those are the expected results.

You neglected to tell us what it was that _you_ were expecting.

What does Perl do with '\r' character?


Nothing.
 
I

i5513

It would be easier for us to help you if you actually said
what it is that you don't understand...
It was my presentation, ok next time I'll be faster asking you.
You should use the CGI.pm module for that.
Ok I'll look for CGI.pm module.
if ($FORM{"text"} =~ /^(\s|\n|\r)*$/)
^^ ^^
^^ ^^ this part will *never* match


These will match exactly the same as the above:

if ($FORM{"text"} =~ /^\s*$/)

if ($FORM{"text"} =~ /^[\r\n\t\f ]*$/)
Ok I didn't remember \s significate
Saying it in Perl is always better than trying to say it in English.

You say that sentence in Perl like this:

my %FORM = ( text => "\r\n" );
or
$FORM{text} = "\r\n";
Ok I don't know english very well, as you can see
Have you seen the Posting Guidelines that are posted here frequently?
Posting Guidelines, for this group or for any group? I read a bit
about general guidelines.
Those are the expected results.
Ok, I now understand it.

Thanks you very much, and excuse me my post's style
 
D

David Efflandt

Hi!. I don't understand ...

At a html page:
<form name="form1" method="POST" action="example.cgi" >
<textarea name="text"></textarea>
</form>

At a cgi script (example.cgi)
(After get parameters)
if ($FORM{"text"} =~ /^(\s|\n|\r)*$/)

matches "\n" (would match even it $FORM{"text"} was undefined, because
you are matching zero or more).
{
print "<HTML>";
if ($FORM{"text"} =~ /\r/)

matches because $FORM{"text"} contains "\r"
{
print "Hey! It is here! <BR> ";
}
if ($FORM{"texto_in"} =~ /^(\s|\n)*$/)

matches zero or more whitespaces or newlines (even if $FORM{"texto_in"} is
undefined)
{
print "Now it isn't here!";
}
print "</HTML>";
exit (1);
}

Well, When I execute (on Windows) this script with one simple return
on textarea. It gives me next result:
Hey! It is here!
Now it isn't here!

In a textarea, the enter key typically returns a CR-LF pair "\015\012"
(regardless of browser OS). Search for 'Newlines' in 'perldoc perlport'
to see why /\n/ may match "\015\012" in DOS/Win.
 
T

Tad McClellan

i5513 said:
Ok I'll look for CGI.pm module.


If you have a fairly recent perl installed, then the CGI module
should already be installed.

What happens when you type these?

perl -v

perldoc CGI

Ok I don't know english very well, as you can see


But that won't matter much if you say as much as possible "in Perl". :)

Posting Guidelines, for this group


Yes. They are posted to the newsgroup 2 times each week...


http://mail.augustmail.com/~tadmc/clpmisc.shtml
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top