String operators

In the Perl language there are available the following string operators:

  • string relational operators

  • string logical operators

  • other string operators

  • string assignment operators

 

The string relational Perl operators compare two strings and determine the validity of a relationship.

Op

Name

Definition

lt

less than

It returns true if the left operand is stringwise less than the right one.

Example:

($v1, $v2) = ("abc", "abz");
if($v1 lt $v2){
 print ("$v1 is less than $v2\n");
}
 

le

less than

or equal to

It returns true if the left operand is stringwise less than or equal to the right one.

Example:

($v1, $v2) = ("abc", "abc");
if($v1 le $v2){
 print("$v1 is less than or equal to $v2");
 print "\n";
}
 

gt

greater than

It returns true if the left operand is stringwise greater than the right one.

Example:

 ($v1, $v2) = ("abc", "abz");
if($v2 gt $v1){
 print ("$v2 is greater than $v1\n");
}
 

ge

greater than or equal to

It returns true if the left operand is stringwise greater than or equal to the right one.

Example:

($v1, $v2) = ("abc", "abc");
if($v1 ge $v2){
 print("$v1 is greater than or equal to $v2");
 print "\n";
}
 

eq

equality

It returns true if the left operand is stringwise equal to the right one.

Example:

($v1, $v2) = ("abc", "abc");
if($v1 eq $v2){
 print ("$v1 is equal to $v2\n");
}
 

ne

not equal to

It returns true if the left operand is stringwise not equal to the right one.

Example:

($v1, $v2) = ("abc", "ab1");
if($v1 ne $v2){
 print ("$v1 is not equal to $v2\n");
}
 

cmp

comparison

It returns -1, 0, or 1 if the left operand is stringwise less than, equal to or greater than the right one.

Example:

($v1, $v2) = ("am", "ak");
$v = $v1 cmp $v2;
print ("$v( expected 1 )\n");
 

 

The string logical Perl operators are generally derived from boolean algebra and they are mainly used to control program flow, finding them as part of an if, a while or some other control statement. See in the table below the logical string operators.

Op

Name

Definition

!

not

It returns true if the operand is a null string or an undefined value and false otherwise.

Example:

 $v1 = !'some string here'; 
 $v2 = !'';
 

The $v1 variable will return false and the $v2 variable will return true.

not

not

The same meaning as above, but it is a lower-precedence version.

 

and,

&&

and

This operator is used to determine if both operands are true.

Example:

 ($v1, $v2) = ('abc', 'abzu');
 $v = $v1 == 'abc' && $v2 == 'abzu';
 print "$v ( expected 1 )";
 

or, ||

or

This operator is used to determine if either of the operands is true.

Example:

 ($v1, $v2) = ('hello', 'good');
 $v = $v1 == 'hello' || $v2 == 'hello'; 
 print "$v ( expected 1 )";
 

xor

exclusive or

It returns true if either but not both of the operands is true.

Example:

 ($v1, $v2) = ('hello', 'good');
 $v = $v1 == 'hello' xor $v2 == 'world';
 print "$v ( expected 1 )";
 

?

conditional operator

This is a ternary operator and it works like an if ... then ... else clause from the C language. If the left operand is true, it will return the central operand, otherwise the right operand.

Example:

 $v = ("abc" eq "abd") ? "It's equal.\n" : 
 "It's not equal. ( expected )\n"; 
 print $v;
 

 

See in the table below other string Perl operators:

Op

Name

Definition

,

comma

In a scalar context, the comma operator evaluates each element from left to right and returns the value of the rightmost element. In a list context, the comma operator separates the elements of a literal list.

Example:

 # scalar context
 $colors = ( 'blue', 'red', 'yellow' ); 
 print "$colors ( expected yellow )\n";
 # list context
 @colors = ( 'blue', 'red', 'yellow' ); 
 print "@colors[1] ( expected red )\n";
 

=>

comma

This operator is a special type of comma, for example ('dog','cat') is similar to (dog=>'cat'). Or it can be used to separate key/value pairs in a hash structure:

Example:

 %petColors = (dog => 'brown', cat => 'white'); 
 

 

-

negation

If the string operand begins with a plus or minus sign, the string negation operator returns a string with the opposite sign.

Example:

 $v = "-abcd";
 print (-$v, "( expected +abcd )\n"); 
 

.

conca-tenation

This operator joins two or more strings like in the below example.

Example:

 $v = "Hello" . " World" . "!"; 
 print $v, " ( expected Hello World! )\n";
 

..

range operator

The range operator, in a list context, produces the range of values from the left value through the right value in increments of 1.

Example:

 @v = ('ab' .. 'ae'); 
 print @v, " ( expected abacadae )\n";
 

x

repetition

The repetition operator returns the first operand (which is a string) repeated by the number of times specified by the second operand (which is an integer).

Example:

 $v = 'ab' x 3;
 print $v, " ( expected ababab )\n";
 

 

String assignment Perl operators perform some type of string operation and then assign the value to the existing variable.

Op

Name

Definition

=

assignment

This is the ordinary assignment operator.

Example:

 $v = 'Hello World!';
 print $v, " ( expected Hello World! )\n";
 

&&=

logical and

It's a combination between the logical "&&" and the assignment operators.

Example:

 $v = 1;
 $v &&= 'abc' eq 'abc';
 print $v, " ( expected 1 )\n";
 

||=

logical or

It's a combination between the logical "||" and the assignment operators.

Example:

 $v = 0;
 $v ||= 'abc' eq 'abc'; 
 print $v, " ( expected 1 )\n";
 

.=

conca-tenation

It's a combination between the concatenation "." and the assignment operators.

Example:

 $v = "Hello ";
 $v .= 'World!'; 
 print $v, " ( expected Hello World! )\n";
 

x=

repetition

It's a repetition assignment operator.

Example:

 $v = "true ";
 $v x= 2;
 print $v, " (expected true true )\n";