Close
Close

Print in Perl


I am assuming that you have gone through the introduction section and know how to write and compile Perl codes.

Now, let's print something on the screen with Perl :

print "Hello World!\n";
Output
Hello World!

So, we have just written a code that can print anything on the screen. Sounds good! Now, let's understand this code in detail so that we can print any message.

To print any message on screen, just type print and that message within double quotes (" "). Isn't it simple? print is a pre-defined function. You will learn to make your own functions in later sections.

In Perl, we end every statement with a semicolon (;). That's why we have used a semicolon (;) after the print statement. It represents the end of the statement.

But why is \n there?


\n is the newline character which is used for changing line. It means that it will print a new line.
In the above example, when print encountered H, it printed H, when e then e. Similarly, when it encountered \n, it printed a new line and that's it.

See this example to make it clearer:

print "Hello\nWorld!\n";
Output
Hello
World!
print "Hey there\n";
print "I wanna be a coder.";
print "Right now, I am learning Perl.\n";
Output
Hey there
I wanna be a coder.Right now, I am learning Perl.

The second statement is without \n and that's why there is no newline after "I wanna be a coder.".

Notice that we have used a semicolon (;) after every statement.

say


In Perl v10, another way to print was introduced with the use of say. say automatically adds a new-line after the end of its arguments. It means that print "Hello\n" and say "Hello" are both the same.

This feature is used after explicitly asking to enable it. For this, we can use use feature ':5.10' in our code. Let's see an example:

use feature ':5.10';
say "Hello";
Output
Hello

You can use any of the two (print or say) in your codes. I will be using 'print' in the rest of our codes.

Whitespaces


Perl doesn't count whitespaces. It will be clear from the example given below:

print       "Hey there\n";



print "I wanna be a coder.\n";
print "Right now, I am learning Perl.\n";
Output
Hey there
I wanna be a coder.
Right now, I am learning Perl.

The code worked fine and this is what I was trying to tell you, that Perl doesn't count spaces in its syntax.

Printing in multiline


Whitespaces between double quotes (" ") are counted in Perl and are displayed as it is.

Let's see this example:

print "Hey
  there\n";
Output
Hey
  there

In the above example, newline and spaces got printed since these are inside double quotes (" ").

Let's write the previous example a bit differently.

print "Hey there
I wanna be a coder
Right now, I am learning Perl.\n";
Output
Hey there
I wanna be a coder.
Right now, I am learning Perl.

We can also use here documents for multiline printing which is explained in the next chapter.

Single quotes and double quotes


Variables and special characters ( like \n ) written between single quotes (' ') are printed as it is and are not interpolated. Let's see an example to understand it.

print "Hey there\n";
print 'Hey there\n';
print '\n\n\n\n\n';
print "\n";
Output
Hey there
Hey there\n\n\n\n\n\n

You can see that '\n' of 'Hey there\n' got printed when written in single quotes (' ') but a new line was printed with double quotes (" ").

Commenting


You can also include something in your code which won't be compiled and your computer will simply ignore that while running your code. Comments are written to make your code more comprehensible while reading. They are written after ‘#’. Comments written after '#' are single line comments. New line will not be a part of the comment

# This is a comment
# This is another comment
print "I have used comments\n";
Output
I have used comments
comments in perl

Why use comments?


As mentioned earlier, it makes your code more readable. Assume that you have written a software and after releasing it, you hired a few good programmers for its maintenance. Without comments, it would be a very difficult job for them to understand your code. Most of the time it happens that the person who has written a code is not the one who is going to modify it. So, make it a habit of writing comments.

Let's do some error analysis


This part is only to teach you how to know the error if you are getting one.
What if we type this :

prin "Hello World\n";

Here, we are trying to teach you how to fix an error, if you got one.

String found where operator expected at a.pl line 1, near "prin "Hello World\n""
       (Do you need to predeclare prin?)
syntax error at a.pl line 1, near "prin "Hello World\n""
Execution of a.pl aborted due to compilation errors.

expected at a.pl line 1 -> a.pl is just the filename. It will be the same as your filename. line 1 is the expected line in which the error may occur. In our code, the error is in line 1.
syntax error at a.pl line 1, near "prin "Hello World\n"" : prin is nothing for our program and hence the syntax is wrong, that's why we got this error.
Go back to your code to that line and fix the error by changing prin to print.

In future, when writing long codes, make sure that you run your code from time to time while writing instead of completing your whole code and running at last. This will make debugging your code easier if you have made some errors.

You will learn more advanced things in the next chapter. So, let's go!


Ask Yours
Post Yours
Doubt? Ask question