Perl

Use of Print in PERL

“Many options exist in PERL to print different types of values and variables. The print operator is one of them. It prints the value that is passed into it as an argument. The double quote (“ “) is used with the print operator to print any data. It can be used to print string values, one or more variables, write into the file content, or read from the file. The uses of this output operator in PERL have been shown in this tutorial.”

Syntax

Different syntaxes of the print operator are given below.

  1. print string_value
    The print operator can be used to print any string value using single-quotes or double-quotes.
  2. print Variable_1, Variable_2,.., Variable_n
    The print operator can be used to print the values of one or more variables with or without other string values.
  3. print File_handler string
    The print operator can be used to read from the file or write into the file.

Return Value

This function returns 0 on failure and 1 on success.

Different uses of the print operator in PERL have been shown in the next part of this tutorial.

Example-1: Print Simple Messages

Create a PERL file with the following code that will print two simple text messages with the newline. The first print operator is used to print the text by using double quotes. The second print operator is used to print the text by using single quotes.

# Print a message using double quotes("")
print("Welcome to Linuxhint\n");
# Print a message using single quotes('')
print('Learn PERL Programming',"\n");

Output:

The following output will appear after executing the above code.

Example-2: Print Multiple String Values

Create a PERL file with the following code to print multiple string values by using comma(,) with a print operator and by using backward slash(\) with two print operators.

# Print multiple string values using separator
print("Welcome to Linuxhint."," ", "Learn PERL programming from Linuxhint.", "\n");
# Print multiple strings using two print() functions
print("Welcome to Linuxhint.");\
print(" Learn PERL programming from Linuxhint.", "\n");

Output:

The following output will appear after executing the above code.

Example-3: Print One or More Variables

One or more variables can be printed by using the print operator with the double quotes (“). Create a PERL file with the following code that will print a single variable, two variables, and the variables with other string values. A single variable has been printed by using double quotes (“) with the print operator. Two string variables have been printed without using any quotes with the print operator. One string and one numeric value have been printed with other string values by using the print operator. Here, backslash(\) has been with the “$” to print “$” in the output because this symbol is used to declare the variable in PERL.

# Define a string variable
$string1 = 'Hello';
# Print the variable
print "$string1\n";

# Define another string variable
$string2 = ' World';
# Print two string variables
print $string1, $string2, "\n";
# Define another string value
$book_name = "PERL Programming";
# Define a numeric value
$price = 35;
# Print the variables with other strings using comma(,)
print "The price of ", $book_name, " book is \$", $price, ".\n";
# Print the variables with other strings using dot(.)
print "The price of ".$book_name." book is \$".$price.".\n";

Output:

The following output will appear after executing the above code.

Example-4: Print the Arithmetic Calculation

The result of any arithmetic calculation can be printed directly by using the print operator. Create a PERL file with the following code that will print the multiplication of two numerical variables and print the result of an arithmetic expression.

# Define two numeric values
$n1 = 45;
$n2 = 5;
# Print the multiplication result of the variables
print "The result of multiplication is ".$n1*$n2."\n";
# Print the calculated result of the mathematical expression
print "The result of the expression = ", 2+6*7, "\n";

Output:

The following output will appear after executing the above code. The multiplication of two variables that have the values 45 and 5 is 225. The result of the arithmetic expression, 2+6*7, is 44.

Example-5: Write Into a File

Create a PERL file with the following code that will open a file for writing and add a line of text by using the print operator. A text file named temp.txt has been opened for writing by using an open function in the code. If the file existed before, then the content of the file will be overwritten by the new content. The file will be closed by using the close function after completing the write operation.

# Open file handler for writing into a file
open fh, ">temp.txt"      
# Print error message
or die "Unable to create the file.";  
# Write the string value into the file
print fh "Add text to the file\n";
# Close the file
close fh                
# Print error message
or die "Unable to close the file.\n";

Output:

The following output will appear after executing the above code. The “cat” command has been executed to check whether the file is created properly or not.

Example-6: Read From a File

Create a PERL file with the following code that will open a file for reading, iterate the content of the file by using a loop, and print each line of the file by using the print operator. A text file named myfile.txt has been opened for reading by using the open function in the code. If the file does not exist, then an error message will be printed. The file will be closed by using the close function after completing the read operation.

# Open file handler for writing into a file
open fh, "< myfile.txt"      
# Print error message
or die "Unable to open the file.";  
# Reading the file
while()
{
    # print the line
    print $_;
}
# Close the file
close fh                
# Print error message
or die "Unable to close the file.\n";

Output:

The following output will appear after executing the above code. The “cat” command has been executed to check the content of the file.

Conclusion

The way of using the print operator in PERL for printing single or multiple string values or variables, reading from a file, and writing into a file has been shown in this tutorial by using multiple examples.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.