Home arrow Perl Programming arrow Page 3 - Scalars and Variables

Variables - 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).

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

Now it is time to talk about variables. As explained earlier, a variable is storage for your scalars. Once youíve calculated 42*7, itís gone. If you want to know what it was, you must do the calculation again. Instead of being able to use the result as a halfway point in more complicated calculations, youíve got to spell it all out in full. Thatís no fun. What we need to be able to do, and what variables allow us to do, is store a scalar away and refer to it again later.

A scalar variable name starts with a dollar sign, for example:$name. Scalar variables can hold either numbers or strings, and are only limited by the size of your computerís memory. To put data into our scalar, we assign the data to it with the assignment operator=. (Incidentally, this is why numeric comparison is==, because= was taken to mean the assignment operator.)

What weíre going to do here is tell Perl that our scalar contains the string"fred". Now we can get at that data by simply using the variableís name:

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

$name = "fred";
print "My name is ", $name, "\n";

Lo and behold, our computer announces to us that

$ perl vars1.pl
My name is fred
$

Now we have somewhere to store our data, and some way to get it back again. The next logical step is to be able to change it.

Modifying a Variable

Modifying the contents of a variable is easy, just assign something different to it. We can say

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

$name = "fred";
print "My name is ",               $name, "\n";
print "It's still ",               $name, "\n";
$name = "bill";
print "Well, actually, now it's ", $name, "\n";
$name = "fred";
print "No, really, now it's ",     $name, "\n";

And watch our computer have an identity crisis:

$ perl vars2.pl
My name is fred
It's still fred
Well, actually, now it's bill
No, really, now it's fred
$

We can also do a calculation in several stages:

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

$a = 6 * 9;
print "Six nines are ", $a, "\n";
$b = $a + 3;
print "Plus three is ", $b, "\n";
$c = $b / 3;
print "All over three is ", $c, "\n";
$d = $c + 1;
print "Add one is ", $d, "\n";
print "\nThose stages again: ", $a, " ", $b, " ", $c, " ", $d, "\n";

This code prints

$ perl vars3.pl
Six nines are 54
Plus three is 57
All over three is 19
Add one is 20
Those stages again: 54 57 19 20
$

While this works perfectly fine, itís often easier to stick with one variable and modify its value, if you donít need to know the stages you went through at the end:

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

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

The assignment operator =, has very low precedence. This means that Perl will do the calculations on the right-hand side of it, including fetching the current value, before assigning the new value. To illustrate this, take a look at the sixth line of our example. Perl takes the current value of $a, adds three to it, and then stores it back in $a.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

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