Perl print() function is one of the most commonly used function by the Perl users. It will print a string, a number, a variable or anything it gets as its arguments.
print "";
Let's see a simple example of Perl Print() function.
#!/usr/bin/perl print "Welcome to rookienerd!!"; print "This is our Perl tutorial!!";
The above output prints both the sentences in the same line. To print them in different lines, we can use "\n" new line character in print function.
The '\n' character is used to print a new line. It is used at the end of the print function.
#!/usr/bin/perl print "Welcome to rookienerd!!\n"; print "This is our Perl tutorial!!\n";
To print the variables value, you need to pass the variable in print function.
#!/usr/bin/perl $site = 'rookienerd'; print "Welcome to $site!!\n"; print "$site provides you all type of tutorials!!\n";
Here, we have defined a variable $site. We have passed this variable in the string which prints its value in the output.
Perl print function does not interpolate inside single quotes. It means it prints the characters inside single quote as it is. It neither evaluates the variable?s value nor does it interpret the escaping characters.
When the above example runs inside a single quote, you will notice the following changes.
#!/usr/bin/perl $site = 'rookienerd'; print 'Welcome to $site!!\n'; print '$site provides you all type of tutorials!!\n';
In the output, variable $site is printed as it is. Its value is not evaluated. The new line character is also not interpreted.
The say() function is not supported by the older perl versions. It acts like print() function with only difference that it automatically adds a new line at the end without mentioning (\n).
say "";
Let's see a simple example of perl say function.
#!/usr/bin/perl use 5.010; say "Welcome to rookienerd!!"; say "This is our Perl tutorial!!";
The above output prints both the sentences in different lines without using the new line character. And here we have used use 5.010 to support say function.