Familiarize yourself with how term, operator, and expression are used in Perl.
Terms, Operators, and Expressions in Perl
Expressions and statements are often the same thing in Perl. Strictly speaking, an expression is anything that has a result, even if that result is the undef value. In other words, just about everything in Perl is an expression.
The undef value is a special value in Perl which represents an variable which is in a uninitialized or undefined state. There is also a corresponding function, undef, that can be used to undefine a variable which already has a value. An operator is a symbol (or a keyword) that operates on various terms to return a result.
The term(s) of the operator are the various expressions on either or both sides of the operator that provide the values on which the operator operates.
Two examples of the undef value using Perl 6
In Perl 6 (now known as Raku), the undef value functions similarly to Perl 5, representing an uninitialized or undefined value. Here are two examples of using undef in Perl 6:
Example 1: Assigning undef to a Variable
In this example, we assign undef to a variable to demonstrate its undefined state.
my $variable = undef;
say $variable; # OUTPUT: (Any)
In Raku, undef returns (Any) when printed, which is the default undefined type.
Example 2: Checking if a Variable is Defined
In this example, we check if a variable is defined using the defined function.
my $another_variable = undef;
if !$another_variable.defined {
say "The variable is undefined.";
} else {
say "The variable has a value.";
}
# OUTPUT: The variable is undefined.
Here, defined checks if $another_variable has been assigned a value. Since itâs set to undef, the condition evaluates as false, confirming that the variable is in an undefined state.
These examples highlight how undef is used to represent undefined states in Raku (Perl 6).
Some operators work with terms on both sides; others work on terms on just one side of the operator. Those that work on just one side are called unary operators. In fact, there is even one operator which works on three terms, which is called a ternary operator.
Often, however, you will have expressions with more than one operator. In those cases your result may well depend on which operator evaluates first! In the next lesson you will learn about the relative precedence of Perl operators.