Home arrow Perl Programming arrow Page 4 - Scalars and Variables

Operating and Assigning at Once - 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



Operations, like fetching a value, modifying it, or storing it, are very common, so there’s a special syntax for them. Generally

$a = $a <some operator> $b;

can be written as

$a <some operator>= $b;

For instance, we could rewrite the preceding example as follows:

#!/usr/bin/perl -w
# vars5.pl

$a = 6 * 9;
print "Six nines are ", $a, "\n";
$a += 3;
print "Plus three is ", $a, "\n";
$a /= 3;
print "All over three is ", $a, "\n";
$a += 1;
print "Add one is ", $a, "\n";

This works for**=,*=,+=,-=,/=,.=,%=,&=,|=,^=,<<=,>>=,&&=, and||=. These all have the same precedence as the assignment operator=.

Autoincrement and Autodecrement

There are also two more operators,++and--. They add and subtract one from the variable, but their precedence is a little strange. When they precede a variable, they act before everything else. If they come afterwards, they act after everything else. Let’s examine these in the following example:

#!/usr/bin/perl -w
# auto1.pl

$a = 4;
$b = 10;
print "Our variables are ", $a, " and ", $b, "\n";
$b = $a++;
print "After incrementing, we have ", $a, " and ", $b, "\n";
$b = ++$a * 2;
print "Now, we have ", $a, " and ", $b, "\n";
$a = --$b + 4;
print "Finally, we have ", $a, " and ", $b, "\n";

You should see the following output:

$ perl auto1.pl
Our variables are 4 and 10
After incrementing, we have 5 and 4
Now, we have 6 and 12
Finally, we have 15 and 11

Let’s work this through a piece at a time. First we set up our variables, giving the values4and10to$aand$brespectively:

$a = 4;
$b = 10;
print "Our variables are ", $a, " and ", $b, "\n";

In the following line, the assignment happens before the increment—this is known as a post-increment. So$bis set to$a’s current value,4, and then$ais autoincremented, becoming 5.

$b = $a++;
print "After incrementing, we have ", $a, " and ", $b, "\n";

In the next line however, the incrementing takes place first—this is known as a pre-increment.$ais now 6, and$bis set to twice that, 12.

$b= ++$a * 2;
print "Now, we have ", $a, " and ", $b, "\n";

Finally,$bis decremented first (a pre-decrement), and becomes 11.$ais set to$bplus 4, which is 15.

$a= --$b + 4;
print "Finally, we have ", $a, " and ", $b, "\n";

The autoincrement operator actually does something interesting if the variable contains a string of only alphabetic characters, followed optionally by numeric characters. Instead of converting to a number, Perl “advances” the variable along the ranges a–z, A–Z, and 0–9. This is more easily understood from a few examples:

#!/usr/bin/perl -w
# auto2.pl

$a = "A9"; print ++$a, "\n";
$a = "bz"; print ++$a, "\n";
$a = "Zz"; print ++$a, "\n";
$a = "z9"; print ++$a, "\n";
$a = "9z"; print ++$a, "\n";

should produce

$ perl auto2.pl

This shows that a 9 turns into a 0 and increments the next digit left. Azturns into ana and increments the next digit left, and if there are no more digits to the left, either ana or anAis created depending on the case of the current leftmost digit.

Please check back next week for the conclusion to this article series.

>>> 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: