Perl | Multi-line Strings | Here Document
Last Updated :
06 Jul, 2018
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
# 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
# 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
# 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.
Similar Reads
Multi-Line Comment in Shell Script
Comments are quite a crucial part of a program or a code-base. It helps in understanding the code, improves code-readability, and also helps in enhancing the structure of the program. We often write single-line comments in our programs as they are quite self-explanatory and require few words to desc
2 min read
How to write Multi-Line Strings in PHP ?
Multi-Line Strings can be written in PHP using the following ways.Using escape sequencesWe can use the \n escape sequences to declare multiple lines in a string.PHP Code:PHP<?php //declaring multiple lines using the new line escape sequence $var="Geeks\nFor\nGeeks"; echo $var; ?>Output:Geeks F
2 min read
How to Create a Multi-Line Comment in R
In R Programming Language multi-line comments are created using the # symbol at the beginning of each line. These comments are typically used for documentation purposes, helping explain your code's functionality. The R interpreter ignores them during code execution and is useful for providing contex
2 min read
How to create multi-line strings in JavaScript ?
In JavaScript, the absence of native support for multi-line strings was a longstanding challenge. However, with the advent of ES6 (ECMAScript 2015), the things changed dramatically. ES6 introduced string literals, offering robust support for multi-line strings, which was a significant enhancement fo
3 min read
How to use Here Document in bash programming
A shell script is a sequence of commands that are written in a plain text file and executed by the shell program in Unix-like operating systems. The shell is a command-line interface that provides users with a powerful way of automating various tasks, including file management, process control, and
7 min read
Shell Scripting - Here Strings
Here String is a kind of string that is used for input redirection from text or a variable. In here string Input will be mentioned in the same line within the quotes. the string inside the quotes is known as the Here string and Its command contains "<<<" (3 less than the symbol). Here strin
2 min read
Line Clamping and Multiple Line Truncating in CSS
Line clamping, or multi-line truncation limits the number of visible lines of text in a block while truncating any additional text that exceeds the specified number of lines. This is useful for maintaining clean layouts and ensuring that excessive text doesn't disrupt the design.There are two common
3 min read
C# Program For Counting Lines in a String
In C#, a string is a sequence of Unicode characters or an array of characters. The range of Unicode characters will be U+0000 to U+FFFF. A string is the representation of the text. In this article, we will create a C# program that will count the lines present in the given string. Example: Input : he
6 min read
Perl | Use of STDIN for Input
Perl allows the programmer to accept input from the user to perform operations on. This makes it easier for the user to give input of its own and not only the one provided as Hardcoded input by the programmer. This Input can then be processed and printed with the use of print() function. Input to a
2 min read
How to Comment and Uncomment multiple line vi terminal editor
The Vi and Vim text editors are powerful tools widely used by programmers, system administrators, and writers for efficient text manipulation and editing. A common task that often arises during coding or text management is the need to comment or uncomment multiple lines of code or text. Commenting h
3 min read