Home arrow Perl Programming arrow Page 2 - Scalars and Variables

Operators to Be Seen Later - Perl

In this fourth part of a five-part series on scalars in Perl, you learn how to compare the value of strings; we'll also wrap up our discusssion of operators and move on to variables. This article is excerpted from chapter two of the book Beginning Perl, written by James Lee (Apress; ISBN: 159059391X).

  1. Scalars and Variables
  2. Operators to Be Seen Later
  3. Variables
  4. Operating and Assigning at Once
By: Apress Publishing
Rating: starstarstarstarstar / 1
April 15, 2010

print this article



There are a few operators left that we are not going to go into in detail right now. Don’t worry, we will eventually come across the more important ones.

  • The conditional operator looks like this:a?b:c. It returnsbifais true, andcif it is false. 
  • The range operators,..and..., make a range of values. For instance, (0..5) is shorthand notation for (0,1,2,3,4,5). 
  • We’ve seen the comma for separating arguments to functions likeprint(). In fact, the comma is an operator that builds a list, andprint()works on a list of arguments. The operator=>works like a comma with certain additional properties.
  • The=~and!~operators are used to “apply” a regular expression to a string. More on these operators in Chapter 7. 
  • As well as providing an escape sequence and backwhacking special characters,\is used to take a reference to a variable, to examine the variable itself rather than its contents. We will discuss this operator in Chapter 11. 
  • The>>and<<operators “shift” a binary number right and left a given number of bits. 
  • ->is an operator used when working with references, covered in Chapter 11.

Operator Precedence

Table 2-1 provides the precedence for all the operators we’ve seen so far, listed in descending order of precedence.

Table 2-1. Operator Precedence  




List operators

Functions that take list arguments


Infix dereference operator



! ~ \

Logical not, bitwise not, reference of

=~ !~

Regex match, negated regex match

* / % x

Multiplication, division, modulus, replication

+ - .

Addition, subtraction, concatenation

<< >>

Left shift, right shift

< > <= >= lt gt le ge

Comparison operators

== != <=> eq ne cmp

More comparison operators


Bitwise and

| ^

Bitwise or, bitwise xor


Logical and


Logical or

.. ...




, =>

List separator


Logical not


Logical and

or xor

Logical or, xor


Remember that if you need to get things done in a different order, you will need to use parenthesis. Also remember that you can use parenthesis even when they’re not strictly necessary, and you should certainly do so to help keep things readable. While Perl knows full well what order to do7+3*2/6-3+5/2&3in, you may find it easier on yourself to spell it out, because next week you may not remember everything you have just written.

>>> More Perl Programming Articles          >>> More By Apress Publishing

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: