Akio Yanagawa
Akio Yanagawa

Reputation: 323

New Line Character in Perl script

Sorry for this novice question, but new line character in Perl script doesn't work. That is, \n and \t in the script below doesn't work at all, and it just displays "Hello Perl! Hello CGI!" in one line. Usually, what is the cause of this phenomenon? Please let me know if anyone knows about it. Thank you very much.

#!/usr/bin/perl

print "Content-Type: text/html; charset=UTF-8\n\n";

print "Hello Perl!\n";
print "Hello \t CGI!";

Upvotes: 0

Views: 9396

Answers (2)

gpwr
gpwr

Reputation: 1025

The line:

print "Content-Type: text/html; charset=UTF-8\n\n";

is telling the web browser that HTML is going to be passed on to it: (This means that you are outputting HTML to the web browser)

print "Hello Perl!\n";
print "Hello \t CGI!";

In HTML \n and \t has no effect. It does not show and won't do anything in the web browser. It won't display in the web browser.

Use <br> instead of \n

You could use &nbsp;&nbsp;&nbsp;&nbsp; instead of \t 

<br> is used for a new line in HTML and &nbsp; is one white space character in HTML. 

So if you are outputting HTML your code will need to look like:

#!/usr/bin/perl

print "Content-Type: text/html; charset=UTF-8\n\n";
print "Hello Perl! <br>";
print "Hello &nbsp;&nbsp;&nbsp;&nbsp; CGI!";

Keep in mind that if you are outputting HTML to a web browser that you need to use proper valid HTML by having tags and tags, etc. but this is out of the scope of this question.

Upvotes: 1

mpapec
mpapec

Reputation: 50657

\n and \t doesn't show in html as you expect. You can however use plain text to check that these chars are in your output,

#!/usr/bin/perl

print "Content-Type: text/plain; charset=UTF-8\n\n";

print "Hello Perl!\n";
print "Hello \t CGI!";

Upvotes: 5

Related Questions