Last Updated:

Expressions and operators in perl

Expressions and operators in perl

An operator is a piece of program text that the interpreter converts into a complete instruction executed by a computer. From the point of view of language syntax (ways of constructing correct constructions recognized by the interpreter), the operator consists of lexemes - minimal units of language that have a certain meaning for the interpreter. A minimum unit of a language is defined as one that cannot be represented by smaller units when it is further parsed. In Perl, tokens can be identifiers, literals, transaction signs, and a delimiter.

We will define all the tokens allowed in the language. Although their semantics (meaning) may not be entirely clear to novice programmers, we will return to some definitions in the following chapters, where we will clarify their syntax and semantics in connection with the input elements of the language. The fact is that, unfortunately, it is impossible to describe the language without references forward.

An identifier is a sequence of letters, numbers, and underscores "_" that begin with a letter or underscore and is used to name variables, functions, subroutines, file descriptors, formats, and labels in a program. The programmer can use any correct identifiers to name the listed program objects, unless they coincide with the language keywords – predefined identifiers that have a special meaning for the Perl interpreter, for example, ifunlessgoto, etc. Examples of correct and incorrect identifiers are presented in Example 2.3.

Example 2.3. Correct and incorrect identifiers.

# Correct identifiers
myName1
my_Name1
_myName__1
# Invalid identifiers
1 myName # Starts with a number.
-myName # Does not start with a letter or underscore character.
my%Name # Invalid character used for identifiers
my # my is a reserved word.


Note Looking ahead, because Perl variable names begin with a special character (" $ ", " @ ", " % ") identifying their type followed by an identifier, in this case the use of an identifier that matches the Perl keyword is legitimate and does not cause an interpreter error. For example, the following variable names are valid: $print, @do, %if, but this practice is not recommended. This note does not apply to identifiers used to name file handles and labels whose names do not begin with specific characters
.

 

A literal, or literal constant, is a symbol or word in a programming language that, unlike a variable, defines its own value, rather than the name of another element of the language. Literal constants are closely related to the data types represented in the language, and are, in fact, their representatives. In Perl, literals are numbers and strings.

123 # Integer.
23.56 # Fixed-point real number.
2E+6 # Real floating point number.
"Language Perl" # String literal.

Operation signs are one or more special characters that define the actions to be performed on quantities called operands. The actions performed are called operations, which can be unary (apply to one operand), binary (apply to two operands), and ternary (three operands are involved).

Example 2.5. Perl operations.

++$n; # Unary operation (++)
23 * $p; # Binary operation (*)
$n >= 3? print "true": print "false"; # Ternary operation (?:)

A separator is a ";" symbol that terminates any statement and communicates this to the interpreter. The use of a delimiter allows you to specify several statements on one line, although this is not accepted in programming practice, as it worsens the readability of the program text.

In an operator, all of its tokens can be separated by any number of space characters, which include the space itself, the tab character, the newline character, the carriage return, and the line transition character. Therefore, a single statement can be written on multiple lines and you do not need to use any continuation character required in other programming languages. For example, the keyboard input assignment statement number 4 in Example 2.1 can also be written as follows:

Example 2.6. Use space characters in the statement.

$name
= <STDIN>
;

You can not use space characters in the operator at all, but to ensure the readability of the program, we recommend separating tokens with one space, especially since there may be situations when the interpreter does not unambiguously distinguish tokens from a continuous stream of characters.

Note
Because whitespace characters are not meaningful in Perl, they are typically used to structure the text of a program, which consists of writing a group of statements that are logically subordinate to some language construct, with some indentation relative to that construct. For example, a similar approach can be applied to a block of statements executed in a loop construct by shifting all of them to the right relative to that construct. Structuring the text of a program contributes to its better reading and is widely practiced by programmers of many programming languages, for example, the C language
.

Operators in Perl can be simple or composite. A simple operator is an expression that ends with a semicolon separator ";", and that is evaluated solely for its side effect. What is a side effect of an expression, we will define a little later, but for now we will focus on the concept of "expression".

An expression is a sequence of literals, variables, and functions connected by one or more operations that compute a scalar or array, that is, when the interpreter processes an expression, the only action is to calculate the value, rather than performing some other action, such as assigning a new value to a variable. When evaluating an expression, there can be side effects when the value of the variable that is part of the expression changes when the expression is evaluated. They can be called, for example, by increment (++) and decrease (--) operations, or by calling a function that changes the value of its actual parameter. As mentioned above, a simple operator is executed to implement this side effect, otherwise what's the point of just calculating an expression whose value cannot be used in any way.

Example 2.7. Simple operators.

++$n; # The value of $n is incremented.
123*$n; # A simple statement with no side effect.

Each simple operator can have a modifier that is placed after the expression before the semicolon. Modifiers are ifunlesswhile, and until keywords followed by an expression. The semantics of using a modifier is that a simple operator is executed if the expression after the modifier is true or false, depending on the modifier used. Modifiers are used in the same way as in ordinary spoken English. For example, a simple operator:

$n++ while <STDIN>;

... will be executed by increasing the value of the variable $n by one each time the user is typing. You can stop this statement by typing CTRL+Z or CTRL+C.


Perl incorporates the best elements of other programming languages, mainly C. The design of modifiers is borrowed from the deceased BASIC/PLUS language of Digital Equipment Corp
.

(All simple operator modifiers are discussed in detail in Part 5)

To define a construct called a composite operator, we will first have to introduce the concept of "block". The sequence of Perl statements that defines the scope of variables is called a block. After getting acquainted with the variables, this definition will not be as vague as it may seem now to a novice programmer. For the purposes of this chapter, it is sufficient to think of a block as a sequence of statements enclosed in curly brackets:

{ operator 1;
operator n; }

A composite operator is defined in terms of a block and can be of one of the following types:

Example 2.8. Composite operators.

if (expression) BLOCK
if (expression) BLOCK_1 else BLOCK_2
if (expression_1) BLOCK_1 elsif (expression_2) BLOCK_2… else BLOCK_p
LABEL while (expression) BLOCK
LABEL while (expression) BLOCK_1 continue FIR_2
LABEL for (expression_1; expression_2; expression_3) BLOCK
LABEL foreach variable (list) BLOCK.LABEL BLOCK_1 continue BLOCK_2

Note that, unlike the C and Pascal programming languages, the compound Perl operators are defined in terms of blocks, not in terms of operators. This means that where a block is needed, it should always be defined using curly braces. In composite operators, even if a block consists of a single operator, it must be enclosed in curly braces. This syntax does not lead to ambiguities and, for example, in nested condition statements, it is always clear which if is consistent with else or elsif. A label that is an identifier with a colon ":" is not required in composite operators, but if it is present, it is relevant for the nextlast, and redo loop control operators.

In this chapter, we learned about the basic syntactic concepts used to form the correct perl constructs. We learned what the main elements of the Perl program consist of, and also developed and executed our first program.