Back

Printing blank lines in Perl

Today I found out that my old XML Online Liner Post appears also with questions such as "how to print one blank line in perl". Of course, XML serializing does not help too much with this beginners' question. But it is dead easy so I write this recipie.

The solution

Either use old school Perl (works even on very, very old servers)

print "\n"; # note the double quotes!

or modern Perl (works practically everywhere but very very old servers).

use v5.10; # you always want to put this line to the beginning of your file!
say;

Explanation

In the old days before perl 5.10 I wrote lines like the one below a lot. 

print "\n"; 

This creates a blank line on the standard output (either the terminal or the stream to the browser if you code web-apps). It looks ugly, because of the backslash, it is hard to type on most keyboards, and it makes it much harder to read the code. 

As of Perl 5.10 one of the cool extensions of Perl 6 became available to the Perl mainstream: Now your program can say things! The say function is a print function that adds a line break after the string the code just printed. It therefore reduces the need of unnecessary double quotes and helps to secure your code. 

Today, with modern perl you like to write the following code. 

use v5.10; # this line goes preferribly to the beginning of your script or module.
say; 

This will create an empty line for you.

Often you also like to add markers such as a line of stars on the output. You can use say for that as well. 

use v5.10;
say 80 x '*'; # creates a line with 80 stars

More details and information

say man page on Perldocs

More Perl stuff here on lo-f.at

Modern Perl Blog

Perl 5.10 features on The Geek Stuff