Last Updated:

List operations | Quotation marks | Perl

A list operation is an operation on a list of values, and the list does not have to be enclosed in parentheses.

We are already familiar with one of these operations – the operation of output to a standard print device. Sometimes we talked about this operation as a function – and rightly so, since all Perl list operations look like function calls (even their description is in the "Functions" section of the Perl documentation), and moreover, they allow you to enclose in parentheses a list of their parameters, which brings them closer to functions. Thus, the print list operation (or function) can be defined in the program in any of the following ways:

print $a, "string", $b; # List operation syntax, print($a, "string", $b);
  # Function call syntax.


A description of many built-in functions (list operations) can be found in the chapters that introduce the language concepts that certain built-in functions are designed to work with. For example, the print, printf, and write functions are described in Chapter 6

Quotation Marks Operations

We use quotation marks (single, double, and inverse) in Perl to set string literals, and the resulting string data depends heavily on the type of quotation marks used: the characters of a string in single quotation marks are treated as they are specified in it, while some characters ($, @) or even sequences of characters (\ , \t) in a string in double quotation marks perform certain actions. The thing is that in Perl, quotation marks are just a convenient syntactic equivalent of certain operations performed on the characters of a string. In addition to the three quoted conclusion operations listed, the language defines a number of operations that perform certain actions on string data and look like quotation marks operations, which we will refer to in the future in the same way as quotation marks operations.

All operations of conclusion in quotation marks are presented in Table. 4.3 with an equivalent syntax (if any) and a brief description of the actions performed when they are performed.

Table 4.3. Quotation marks.

General formEquivalent formMeaningSubstitution capability
q{ }* iString literalNo
qq{ }it ii'String literalYes
qx{ } System CommandYes
qw { }0List of wordsNo
m{ }//Search by patternYes
qr{ } SampleYes
s { } { } SubstitutionYes
tr{ }{ }u///TransliterationNo

When using the general form of the quotation marks operation instead of the curly braces {} represented in Table. 4.3, you can use any character pair selected as a separator. If the selected character is not a parenthesis (round, angular, square, or curly), it is placed at the beginning and end of the string to which the corresponding operation should be applied, whereas in the case of separator brackets, the opening bracket is used first and the closing parenthesis is used at the end. There can be an arbitrary number of space characters between the operation mark and the strings in the delimiter characters. Usually, Perl programmers use the oblique string "/" as a separator, although this is not necessary.

In Table. 4.3 The last column also indicates whether the corresponding operation substitutes the values of scalar variables and arrays, as well as the interpretation of control characters.

In this paragraph, we will focus only on the first four quotation marks. The remaining operations, as directly related to regular expressions, will be discussed in detail in Chapter 10.

Operation q{}

This operation is similar to specifying a string literal in single quotation marks. In it, each character of the string represents itself, the substitution of variable values is not performed. The only exception is a backslash followed by a separator character or another backslash. These character sequences allow you to type a separator character or a backslash directly into a string (even though the backslash is already self-explanatory). A few examples:

q-cDescriptor \<FILE\"; # Character string: Descriptor <FILE>
e! Directory \\bin\usr\n!; # String of characters; Directory \bin\usr\n
'Directory \\bin\usr\n'; # Equivalent to previous operation

Operation qq{}

This operation is similar to specifying a string literal in double quotation marks. When it is executed, scalar variables starting with the $ symbol and scalar array variables starting with the @ symbol are substituted into a string of values, and control sequences are interpreted. After you complete these steps, string data is generated. To specify a delimiter character in a string to use in this operation, you can use a backslash before that character. A few examples:

qq(print\(\) – output function); # Character string:
# print() - output function $t = 123; qq/4enoe\t$m\n/; # Character string:
# Integer 123 "Ifenoe\t$m\n"; # Equivalent to previous operation

Qx{} operation

This operation is similar to specifying a string literal in reverse quotation marks. When it is calculated, the values of scalar variables and scalar array variables (if any) are first substituted into a string specified between the delimiters of the operation, and then the resulting string, as a kind of command, is passed to the operating system command interpreter for execution and the result of its execution is substituted into the qx{} operation generated by the operation. The final string value. In this way, you can enter into a Perl program the results of executing certain commands or user programs. A few examples:

$file = "file.tmp";
qxfdel$file); # Deleting a file named file.tmp
$rez = qx(progl - a); # Variable
$rez contains output results
# to the progl program screen
$rez = 'progl - a~; # Equivalent to previous operation

Operation qw{}

This operation returns a list of words selected from the string specified between the delimiters of the operation. The delimiters between words are considered to be space characters:

@m = qw(one two); I Equivalent: $m[0] = 'one'; $m[l] = 'two';

The action of the qw{STRING} operation is equivalent to the action of the built-in function:


The most common mistake when using this operation is to separate words with commas. When warnings– w are enabled, a message will be generated stating that the comma may be used to separate words instead of being part of the word.

Operation "document here"

Perl implements another interesting feature of "entering" string data into a program that is based on the syntax of the document here by the UNIX shell command interpreter. It allows you to define large string data in the program, located in several consecutive lines of program text, and use them as operands of various operations: assignment, printing, etc.

Its syntax is simple: after the operation sign " is set, the final identifier is specified, which indicates the end of the string data job. This means that all rows of data located between the current row containing the "document here" operation and the row containing the final identifier are treated as a single piece of string data:

$multi_line_string = "LINES; line 1 line 2 LINES 

In the following code snippet, the scalar variable $muiti_iine_string will contain the string "string 1\string 2\p". As you can see, the characters entered from the keyboard are saved when using the "document here" operation. By default, the "document here" operation interprets the contents of all program strings up to the final identifier as string data enclosed in double quotation marks, retaining the character "\n" in them. Perl allows you to explicitly specify how the data will be interpreted during this operation by enclosing the final id of the operation in double quotation marks. The following two "document here" operations are equivalent:

print << m;
line 1
print << "m";
line 1

The identifier can be specified in both single quotation marks and back quotation marks. In this case, the contents of subsequent program lines up to the string that contains the final identifier are treated as string data in the appropriate quotation marks.

In a string that physically restricts the document here data, the final identifier is set without any quotation marks.

When you specify a final quotation mark identifier, string data is subject to all variable substitution and control character rules that are applied to strings bounded by the corresponding type of quotation marks. Example 4.16 demonstrates the use of different quotation marks in a "document here" operation.

#! peri-w
$var = "Alexander"; print << FIRST; # Display: User:
# User: \t$var # Alexander FIRST #
print "'FIRST'; # Displays:
User: I User:
\t$var # \t$var
$coml = "echo Alex";
print << 'FIRST'; # Will display:
$coml i Alex FIRST #.


Note that example 4.16 used the same final ID FIRST. This does not lead to ambiguities and compilation errors, because the compiler looks for the first string with a final identifier after the << operation. The main thing is that the final identifier in the completion line is set exactly as it is specified in the operation itself.

when you use a "document here" operation with a trailing id in back quotation marks some operating systems may have a problem with processing a command flow that is defined on multiple lines. not all command prompts can handle multiple command lines. typically they are focused on typing a command at the input prompt executing it, and waiting for the next command input. some command shells may handle multiple command lines commands specified on a single line through a delimiter, such as the Windows NT cmd command interpreter, in which the delimiter is the & character

If the final identifier in the operation " is specified without quotation marks, it must follow the character of the operation without any spaces. If this happens, Perl interprets this operation with the final identifier of the empty string "" and looks in the program text for the first empty string that limits the string data of this operation:

$var = "Alexander";
print << x2; i Display the following 3 lines 2 times:
User: # User:
\t$var # Alexander
x2 1x2
# An empty string terminates the operation <<

In this code snippet, a space is incorrectly placed before the trailing identifier x2. The compiler parsed the line with the print print operation as follows: the string data entered by the << operation is terminated with an empty line, after which they are simply repeated 2 times (the sequence of characters x2 is understood as the operation of repeating the line x with the right operand equal to 2).

This example is selected specifically in such a way that it is properly compiled. If you put, for example, FIRST, instead of the x2 identifier, the compiler will generate an error.

The result of a document here operation can be used as an operand for string operations. You can even use multiple operations " in one statement, arranging the rows of their data sequentially under each other, not forgetting, of course, the rows with the final identifier:

$stack = <<"ONE". <<"TWO";
The first

The value of the scalar variable $stack will be the following line: