Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.2 Expressions, Statements, and Side-Effects

Before we begin introduce more Perl code examples, we want to explain the ideas of an expression and a statement, and how each looks in Perl.

Any valid "chunk" of Perl code can be considered an expression. That expression always evaluates to some value. Sometimes, the value to which expression evaluates is of interest to us, and sometimes it is not. However, we always must be aware that each expression has some "value" that is the evaluation of that expression.

Zero or more expressions to make a statement in Perl. Statements in Perl end with a semi-colon. For example, in the Perl code we saw before, we turned the expression, chomp($userName), into a statement, chomp($userName); by adding a ; to the end. If it helps, you can think about the ;s as separating sets of expressions that you want Perl to evaluate and execute in order.

Given that every expression, even when combined into statements, evaluate to some value, you might be tempted to ask: What does the expression chomp($userName) evaluate to? It turns out that expression evaluates to the total number of characters removed from the end of the variable $userName. This is actually one of those cases where we are not particularly interested in the evaluation result of the code. In this case, we were instead interested in what is called the side-effect of the expression.

The side-effect of an expression is some change that occurs as a result of that expression's evaluation. Often, a side-effect causes some change in the state of the running program, such as changing the value of a variable. In the expression chomp($userName), the side-effect is that any newline characters are removed from the end of the variable, @scalar{$username}.

Let's now consider a slightly more complex statement, and look for the the expressions and side-effect. Consider the statement, $username = <STDIN>; from our first program. In this case, we used the expression, <STDIN> as part of a larger expression, namely $username = <STDIN>. The expression, <STDIN> evaluated to a scalar value, namely a string that represented a line from the standard input. It was of particular interest to us the value to which <STDIN> evaluated, because we wanted to save that value in the variable, @scalar{$username}.

To cause that assignment to take place, we used the larger expression, $username = <STDIN>. The side-effect of that larger expression is that @scalar{$username} contains the value that <STDIN> evaluated to. That side-effect is what we wanted in this case, and we ignore the value to which $username = <STDIN> evaluates. (It turns out that it evaluates to the value contained in $username after the assignment took place.)

The concepts of statements, expressions and side-effects will become more clear as we continue. When appropriate, we'll point out various expression and discuss what they evaluate to, and indicate what side-effects are of interest to us.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]


 
 
  Published under the terms of the GNU General Public License Design by Interspire