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 helloWorld.pl 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!
#pragma 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("\n"); print(0 .. $var3); print("\n"); print('a' .. 'z'); print("\n");
Hello World! Hello World! $var1 $var2 \n Hello Hello Hello Hello 0123456789 0123456789 abcdefghijklmnopqrstuvwxyz
Well done, you made it!