Perl | Multi-line Strings | Here Document
A string in Perl is a scalar variable and it can contain alphabets, numbers, special characters. The string can consist of a single word, a group of words or a multi-line paragraph. Multi-line string is required in a scenario where the user wants a complete paragraph or a group of paragraphs as a string and for that, he or she needs to preserve the spaces and newline. Multi-line string can be created using various ways.
Multiline String using Single & Double Quotes
User can create a multiline string using the single(”) quotes and as well as with double quotes(“”). Using double quotes cause variables embedded in the string to be replaced by their content while in single quotes variables name remained the same.
Example:
# Perl code to illustrate the multiline # string using single quotes and # double quotes # consider a string scalar $GeeksforGeeks = 'GFG' ; # multiline string using double quotes $mline = "This is multiline string using double quotes on $GeeksforGeeks "; # displaying result print "$mline\n\n\n\n" ; # multiline string using single quotes $multi_line = 'This is multiline string using single quotes on $GeeksforGeeks '; # displaying result print "$multi_line\n" ; |
Output:
This is multiline string using double quotes on GFG This is multiline string using single quotes on $GeeksforGeeks
Multi-line string using Here Document
Here Document is an alternative way for multiple print statements. A Here-Document can also be used for multi-line string. It declares a delimiter at the start to know till where the string needs to take input. A Here-Document starts with ‘<<' followed by the delimiter of the user’s choice. The string reads the code until the delimiter occurs again and all the text in between is taken as the string.
Example:
# Perl code to illustrate the multiline # string using Here-Document # consider a string scalar $GeeksforGeeks = 'GFG' ; # defining multiline string using # ending delimiter without any quotes $deli = <<string_ending_delimiter; Multiline string using Here-Document on $GeeksforGeeks string_ending_delimiter # displaying result print "$deli\n\n" ; # defining multiline string using # ending delimiter with double quotes $deli = << "string_ending_delimiter" ; Multiline string using Here-Document on $GeeksforGeeks string_ending_delimiter # displaying result print "$deli\n\n" ; # defining multiline string using # ending delimiter with single quotes $deli = << 'string_ending_delimiter' ; Multiline string using Here-Document on $GeeksforGeeks string_ending_delimiter # displaying result print "$deli\n\n" ; |
Output:
Multiline string using Here-Document on GFG Multiline string using Here-Document on GFG Multiline string using Here-Document on $GeeksforGeeks
Explanation: In the above code if the delimiter put into double quotes then the variables embedded in the string will replace by their content like variable GeeksforGeeks replaced by GFG and this termed as the Interpolating Here Document. If the delimiter put into single quotes then the variables embedded in the string will not replace by their content and this is termed as the Non-interpolating Here Document. Remember if the delimiter is not put into any quotes then by default double quotes are considered around that.
Note: It is recommended to use the same delimiter at the end of the string exactly as it was at the beginning means there should be no white-spaces before, and no white spaces after the delimiter. Otherwise, Perl will not recognize it and will give the error. In other words, the user cannot indent the end tag to match the indentation of the rest of your code.
Example:
# Perl code to illustrate the error by # changing the ending delimiter # consider a string scalar $GeeksforGeeks = 'GFG' ; # defining multiline string using # ending delimiter without any quotes $deli = <<string_ending_delimiter; Multiline string using Here-Document on $GeeksforGeeks string_ending_delimiter |
Runtime Error:
Can’t find string terminator “string_ending_delimiter” anywhere before EOF at /home/1873b0a5dae105b4bfa82e3c79f156c5.pl line 9.