Perl Operators   «Prev  Next»
Lesson 1

Perl Operator Introduction

Perl Operator Source

Like a lot of the Perl language, many of its operators have been borrowed from a number of different sources, including C, Bourne shell, and Fortran.
Perl gets most of its operators from C, including the increment operators complete with their unique prefix and postfix distinctions. Perl also has unique string operators, including override operators to create your own quotation marks for cases where your strings will have quotes imbedded within them. There are also special operators for dealing with regular expressions, files, and lists, filling out its menu of operators as one of the most complete in any language.

Perl Operators

The operators in a computer language tell the computer what actions to perform. Perl has more operators than most languages and you have already seen some operators-like the equals or assignment operator(=). As you read about the other operators, you will undoubtedly realize that you are familiar with some of them. Trust your intuition; the definitions that you already know will probably still be true.
Operators are instructions you give to the computer so that it can perform some task or operation. All operators cause actions to be performed on operands. An operand can be anything that you perform an operation on. In practical terms, any particular operand will be a literal, a variable, or an expression. You have already been introduced to literals and variables. A good working definition of expression is some combination of operators and operands that are evaluated as a unit.
Operands are also recursive in nature. In Perl, the expression 3 + 5-two operands and a plus operator-can be considered as one operand with a value of 8. For instance, (3 + 5) - 12 is an expression that consists of two operands, the second of which is subtracted from the first. The first operand is (3 + 5) and the second operand is 12. This module will discuss most of the operators available to you in Perl . You will find out about many operator types and how to determine their order of precedence. Precedence is very important in every computer language including Perl .
This module will prepare you to operate on the Perl variables that you learned about in the previous module. With a good understanding of the operations available in the language, you will be able to choose the best operator for a given situation or requirement. To be able to effectively write programs that work exactly how you want them to, you need to have a good grasp of what facilities a language offers, and how to use them. The Perl language has a rich selection of operators, and if you take the time to understand them you will be rewarded every time you write a Perl program.

A variable is just a place to store a value, so you can refer to it or manipulate it throughout your program. Perl has three types of variables:
  1. scalars,
  2. arrays, and
  3. hashes.

A scalar variable stores a single (scalar) value. Perl scalar names are prefixed with a dollar sign ($), so for example, $x, $y, $z, and $url are all examples of scalar variable names. Here's how variables are used:
$num = 1;
$name = "Tom";
$pi = 3.1415927;

Variable Declaration

You do not need to declare a variable before using it.
A scalar can hold data of any type, be it a string or number.
You can also drop scalars into double-quoted strings:
$prime = 23;
$text = "The magic number is $prime.";

Now if you print $text, you will get "The magic number is 23." Let's edit again and add some scalars to it:
$classname = "Basic Perl Programming";
print "Hello there.  What is your name?\n";
$you = <STDIN>
print "Hello, $you.  Welcome to $classname.\n";