Perl - Native functions - Using print()

The Perl print() function is used to display a string.

In our example, we are going to display the content of two variables previously declared and initialized.

So, let's go to our first Perl HelloWorld! tutorial!


We have to create a file named because we will use this file with the perl command.

All comments are beginning by a sharp (#).

The code is quite easy, we have two pragmas (strict and warnings).

A pragma tells the compiler which kind of rules we want to use in our code.

In our example, I'm using the strict pragma to tell: "I want the strict using of Perl, so if you see something wrong, just tell me and oblige me to be strict, please."

The warnings one is the same but to display warnings.

Then I'm declaring two variables. We have to use the my keyword before each variable, because we specified the strict pragma.

In the classic print() examples, we can use the double quote (") to display the content of variables.
If we use the simple quote ('), the name of the variables is displayed.
Finally we're displaying the two variables with the dot (.) in order to concatenate the string into something readable by a human being.

Notice that the first and the second have the same output. Choose which one you prefer.

In the specific print() examples, we use the "x 4" to display the content of $var1 four times.

We'are displaying, next, numbers from 0 to 9 with double dot (..). We can do the same with a variable. And even with letters!

The code

use strict;
use warnings;

# Variable declaration and initialization
my $var1 = "Hello";
my $var2 = "World!";
my $var3 = 9;

# Using classic print() function
print($var1 . " " . $var2 . "\n");
print("$var1 $var2 \n");
print('$var1 $var2 \n' . "\n");

# Using specific print() function
print("$var1\n" x 4);
print(0 .. 9);
print(0 .. $var3);
print('a' .. 'z');

The command line


The output

Hello World!
Hello World!
$var1 $var2 \n


Well done, you made it! angel

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.