For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > Where are These Carriage Returns Coming From?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Where are These Carriage Returns Coming From?
Chris Share

2006-10-20, 3:58 am

Hi,

In the output of the following code there's a carriage return between
the $name variable and the "!". Where is this coming from? Doesn't the
chomp get rid of this?


#!/usr/local/bin/perl

$| = 1;

use strict;
use warnings;
use CGI qw(:standard);

print "What is your name? ";
my $name = <STDIN>;
chomp $name;
print header;
print start_html(-title=>"Hello $name!\n", -bgcolor=>"#cccccc",
-text=>"#999999");
print "<h2>Hello, $name!</h2>\n";
print end_html;


Here's the output:

What is your name? Chris
Content-Type: text/html; charset=ISO-8859-1

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Hello Chris
!
</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body bgcolor="#cccccc" text="#999999">
<h2>Hello, Chris
!</h2>

</body>
</html>
Xavier Noria

2006-10-20, 7:57 am

On Oct 20, 2006, at 11:54 AM, Chris Share wrote:

> In the output of the following code there's a carriage return
> between the $name variable and the "!". Where is this coming from?
> Doesn't the chomp get rid of this?
>
>
> #!/usr/local/bin/perl
>
> $| = 1;
>
> use strict;
> use warnings;
> use CGI qw(:standard);
>
> print "What is your name? ";
> my $name = <STDIN>;
> chomp $name;
> print header;
> print start_html(-title=>"Hello $name!\n", -bgcolor=>"#cccccc", -
> text=>"#999999");
> print "<h2>Hello, $name!</h2>\n";
> print end_html;
>
>
> Here's the output:
>
> What is your name? Chris
> Content-Type: text/html; charset=ISO-8859-1
>
> <!DOCTYPE html
> PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"
> xml:lang="en-US">
> <head>
> <title>Hello Chris
> !


Looks like CGI.pm puts STDIN in binmode on Windows:

$needs_binmode = $OS=~/^(WINDOWS|DOS|OS2|MSWin|CYGWIN)/;
# ...
if ($needs_binmode) {
$CGI::DefaultClass->binmode(\*main::STDOUT);
$CGI::DefaultClass->binmode(\*main::STDIN);
$CGI::DefaultClass->binmode(\*main::STDERR);
}

If that is correct the CRLF -> LF translation of the I/O layer is
disabled and chomp does not remove CR (because $/ is "\n" by
default). Once you see why it is working that way I guess you can
change your code accordingly.

-- fxn

Chris Share

2006-10-20, 7:57 am

Sorry, I don't get it. Could you elaborate?

bou, hou (GE Money, consultant) wrote:
> under is the result of the source.
> chomp $name;
> just delete the CR/LF of the row What is your name? peng CR/LF
> --------------------------------------------------------------------------------------------
> What is your name? peng
> Content-Type: text/html; charset=ISO-8859-1
>
> <?xml version="1.0" encoding="iso-8859-1"?>
> <!DOCTYPE html
> PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"><head><title>Hello peng
> !
> </title>
> </head><body bgcolor="#cccccc" text="#999999"><h2>Hello, peng
> !</h2>
> </body></html>
> --------------------------------------------------------------------------------------------
>
> -----Original Message-----
> From: Chris Share [mailto:cshare01@qub.ac.uk]
> Sent: Friday, October 20, 2006 6:54 PM
> To: beginners@perl.org
> Subject: Where are These Carriage Returns Coming From?
>
>
> Hi,
>
> In the output of the following code there's a carriage return between
> the $name variable and the "!". Where is this coming from? Doesn't the
> chomp get rid of this?
>
>
> #!/usr/local/bin/perl
>
> $| = 1;
>
> use strict;
> use warnings;
> use CGI qw(:standard);
>
> print "What is your name? ";
> my $name = <STDIN>;
> chomp $name;
> print header;
> print start_html(-title=>"Hello $name!\n", -bgcolor=>"#cccccc",
> -text=>"#999999");
> print "<h2>Hello, $name!</h2>\n";
> print end_html;
>
>
> Here's the output:
>
> What is your name? Chris
> Content-Type: text/html; charset=ISO-8859-1
>
> <!DOCTYPE html
> PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
> <head>
> <title>Hello Chris
> !
> </title>
> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
> </head>
> <body bgcolor="#cccccc" text="#999999">
> <h2>Hello, Chris
> !</h2>
>
> </body>
> </html>
>


Hou Bou

2006-10-20, 7:57 am

under is the result of the source.
chomp $name;=20
just delete the CR/LF of the row What is your name? peng CR/LF
-------------------------------------------------------------------------=
-------------------
What is your name? peng
Content-Type: text/html; charset=3DISO-8859-1

<?xml version=3D"1.0" encoding=3D"iso-8859-1"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=3D"http://www.w3.org/1999/xhtml" =
lang=3D"en-US"><head><title>Hello peng
!
</title>
</head><body bgcolor=3D"#cccccc" text=3D"#999999"><h2>Hello, peng
!</h2>
</body></html>
-------------------------------------------------------------------------=
-------------------

-----Original Message-----
From: Chris Share [mailto:cshare01@qub.ac.uk]
Sent: Friday, October 20, 2006 6:54 PM
To: beginners@perl.org
Subject: Where are These Carriage Returns Coming From?


Hi,

In the output of the following code there's a carriage return between=20
the $name variable and the "!". Where is this coming from? Doesn't the=20
chomp get rid of this?


#!/usr/local/bin/perl

$| =3D 1;

use strict;
use warnings;
use CGI qw(:standard);

print "What is your name? ";
my $name =3D <STDIN>;
chomp $name;
print header;
print start_html(-title=3D>"Hello $name!\n", -bgcolor=3D>"#cccccc",=20
-text=3D>"#999999");
print "<h2>Hello, $name!</h2>\n";
print end_html;


Here's the output:

What is your name? Chris
Content-Type: text/html; charset=3DISO-8859-1

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=3D"http://www.w3.org/1999/xhtml" lang=3D"en-US" =
xml:lang=3D"en-US">
<head>
<title>Hello Chris
!
</title>
<meta http-equiv=3D"Content-Type" content=3D"text/html; =
charset=3Diso-8859-1" />
</head>
<body bgcolor=3D"#cccccc" text=3D"#999999">
<h2>Hello, Chris
!</h2>

</body>
</html>

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Krishnakumar K P

2006-10-20, 7:57 am





-----Original Message-----
From: Chris Share [mailto:cshare01@qub.ac.uk]
Sent: Friday, October 20, 2006 3:24 PM
To: beginners@perl.org
Subject: Where are These Carriage Returns Coming From?


Hi,

In the output of the following code there's a carriage return between=20
the $name variable and the "!". Where is this coming from? Doesn't the=20
chomp get rid of this?


#!/usr/local/bin/perl

$| =3D 1;

use strict;
use warnings;
use CGI qw(:standard);

print "What is your name? ";
my $name =3D <STDIN>;
chomp $name;
print header;
print start_html(-title=3D>"Hello $name!\n", -bgcolor=3D>"#cccccc",=20
-text=3D>"#999999");
print "<h2>Hello, $name!</h2>\n";
print end_html;


Here's the output:

What is your name? Chris
Content-Type: text/html; charset=3DISO-8859-1

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=3D"http://www.w3.org/1999/xhtml" lang=3D"en-US" =
xml:lang=3D"en-US">
<head>
<title>Hello Chris
!
</title>
<meta http-equiv=3D"Content-Type" content=3D"text/html; =
charset=3Diso-8859-1" />
</head>
<body bgcolor=3D"#cccccc" text=3D"#999999">
<h2>Hello, Chris
!</h2>

</body>
</html>


Remove the "\n" that you are printing after $name in both the lines.

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>



"Legal Disclaimer: This electronic message and all contents contain =
information from Cybage Software Private Limited which may be =
privileged, confidential, or otherwise protected from disclosure. The =
information is intended to be for the addressee(s) only. If you are not =
an addressee, any disclosure, copy, distribution, or use of the contents =
of this message is strictly prohibited. If you have received this =
electronic message in error please notify the sender by reply e-mail to =
and destroy the original message and all copies. Cybage has taken every =
reasonable precaution to minimize the risk of malicious content in the =
mail, but is not liable for any damage you may sustain as a result of =
any malicious content in this e-mail. You should carry out your own =
malicious content checks before opening the e-mail or attachment."
www.cybage.com=20


Chris Share

2006-10-20, 7:57 am

Krishnakumar K P wrote:
>
>
>
> -----Original Message-----
> From: Chris Share [mailto:cshare01@qub.ac.uk]
> Sent: Friday, October 20, 2006 3:24 PM
> To: beginners@perl.org
> Subject: Where are These Carriage Returns Coming From?
>
>
> Hi,
>
> In the output of the following code there's a carriage return between
> the $name variable and the "!". Where is this coming from? Doesn't the
> chomp get rid of this?
>
>
> #!/usr/local/bin/perl
>
> $| = 1;
>
> use strict;
> use warnings;
> use CGI qw(:standard);
>
> print "What is your name? ";
> my $name = <STDIN>;
> chomp $name;
> print header;
> print start_html(-title=>"Hello $name!\n", -bgcolor=>"#cccccc",
> -text=>"#999999");
> print "<h2>Hello, $name!</h2>\n";
> print end_html;
>
>
> Here's the output:
>
> What is your name? Chris
> Content-Type: text/html; charset=ISO-8859-1
>
> <!DOCTYPE html
> PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
> <head>
> <title>Hello Chris
> !
> </title>
> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
> </head>
> <body bgcolor="#cccccc" text="#999999">
> <h2>Hello, Chris
> !</h2>
>
> </body>
> </html>
>
>
> Remove the "\n" that you are printing after $name in both the lines.
>


I think you've misunderstood my question. The carriage return is between
the $name variable and the "!".

Removing the "\n" after "!" in both the lines doesn't change anything.


Mumia W.

2006-10-20, 6:56 pm

On 10/20/2006 05:35 AM, Xavier Noria wrote:
> On Oct 20, 2006, at 11:54 AM, Chris Share wrote:
>
>
> Looks like CGI.pm puts STDIN in binmode on Windows:
>
> $needs_binmode = $OS=~/^(WINDOWS|DOS|OS2|MSWin|CYGWIN)/;
> # ...
> if ($needs_binmode) {
> $CGI::DefaultClass->binmode(\*main::STDOUT);
> $CGI::DefaultClass->binmode(\*main::STDIN);
> $CGI::DefaultClass->binmode(\*main::STDERR);
> }
>
> If that is correct the CRLF -> LF translation of the I/O layer is
> disabled and chomp does not remove CR (because $/ is "\n" by default).
> Once you see why it is working that way I guess you can change your code
> accordingly.
>
> -- fxn
>


IOW, Chris would set $/ to "\r\n" before performing the chomp.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com