Expressions

Domain Hosting image
Web Hosting
Dedicated server
ssl certificate
Web Design image
Email

If you look at a Perl program from a very high level, it is made of statements. Statements are a complete unit of instruction for the computer to process. Statements can be very simple or very complex. The simplest statement is this 123;

which is a numeric I iteraf for lowed by a semicolon. The semicolon is very important. It tells Perl that the statement is complete. A more complicated statement might be $bookSize = ($numOfPages >= 1200 ? "Large" : "Normal"); which says if the number of pages is 1,200 or greater, then assign "Large" to $bookSize; otherwise, assign "Normal" to $bookSize.

In Perl, every statement has a value. In the first example, the value of the statement is 123. In the second example, the value of the statement could be either " Large " or " Normal " depending on the value of $numOf Pages. The last value that is evaluated becomes the value for the statement.

Understanding Expressions

You can break the universe of expressions up into four types: Simple Expressions
Simple Expressions with Side Effects
Simple Expression with Operators
Complex Expressions

Simple expressions consist of a single literal or variable.

123 - Integer literal
Chocolate is great! - String literal
(1, 2, 3) - Array literal $numPages - Variable

Simple expressions with side effects are the next type of expression we'll examine. A side effect is when a variable's value is changed by the expression. Side effects can be caused using any of the unary operators: +, ++, . These operators have the effect of changing the value of a variable just by the evaluation of the expression.No other Perl operators have this effect other than the assignment operators, of course.

Perl Expression with Side Effects

$numPages++ - Increments a variable
++$numPages - Increments a variable
chop($firstVar) - Changes the value of $firstVar

Note that when the expressions $numPages++ and ++$numPages are evaluated, they have the same side effect even though they evaluate to different values. The first evaluate to $numPages, and the second evaluates to $numPages + 1. The side effect is to increment $numPages by 1.

Simple expressions with operators are expressions that include one operator and two operands. Any of Perl's binary operators can be used in this type of expression.

Perl Expression with Operators 10 + $firstVar - Adds ten to $firstVar
$firstVar . "AAA" - Concatenates $firstVar and "AAA"
"ABC" x 5 - Repeats "ABC" five times

Another way of viewing 10 + $firstVar is as simple expression plus simple expression. Thus, you can say that a simple expression with an operator is defined as two simple expressions connected by an operator. When computer programmers define something in terms of itself, we call it recursion. Each time a recursion is done, the expression is broken down into simpler and simpler pieces until the computer can evaluate the pieces properly.

A complex expression can use any number of literals, variables, operators, and functions in any sequence.

Complex Expression
(10 + 2) + 20 / (5 * * 2)
20 (($numPages-1) * 2)
(($numPages++ / numChapters) * (1.5 / log(10)) + 6)



Domain Name Search

www.


Copyright (C) 2007. Web Domain design hosting. All rights reserved.