How to use special operators

See in the table below the special operators:

Op

Name

Definition

\

reference

We call reference the scalar value that contains a memory address. In order to reference a variable, we use this operator. Look at the below code snippet to see how to use it.

Example:

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

->

dereference

The dereference operator was used for the first time in the Perl 5 language. This operator let us access and manipulate the elements of an array, hash, object of a class data structure. If you use the reference operator to reference a scalar variable, before you use it you must dereference the reference variable.

Example:

@v = ('black', 'white', 'blue', 'orange');
$vRef = \@v; # the reference variable
print $v[1], " ( expected white )\n";
 
$vRef->[1] = 'red'; # dereference before use
print $v[1], " ( expected red )\n";
 

=~

pattern binding

This binary operator binds a string expression to a pattern match. The string which is intend to bind is put on the left meanwhile the operator itself is put on the right. We use the pattern binding operator in the case we have a string which is not stored in the $_ variable and we need to perform some matches or substitutions of that string.

Example:

$v = "black and white";
if($v =~ m/white/) {
 print "Yes ( expected )\n";
}
$v =~ s/black and white/red/;
print $v, " ( expected red )\n";
 

!~

pattern binding

(not)

This operator is similar the operator above, but the return value is negated logically.

Example:

$v = "black and white";
if($v !~ m/yellow/) {
 print "Yes ( expected )\n";
}