Home arrow Perl Programming arrow Scalars: Building a Currency Converter

Scalars: Building a Currency Converter

In this conclusion to a five-part series on scalars in Perl, we'll put everything we've learned together to build a currency converter. This article is excerpted from chapter two of the book Beginning Perl, written by James Lee (Apress; ISBN: 159059391X).

TABLE OF CONTENTS:
  1. Scalars: Building a Currency Converter
  2. Variable Names
  3. Currency Converter
  4. The chomp() and chop() Functions
  5. The die() Function
By: Apress Publishing
Rating: starstarstarstarstar / 2
April 22, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Multiple Assignments

We’ve said that=is an operator, but does that mean it returns a value? Well, actually it does, it returns whatever was assigned. This allows us to set several variables up at once. Here’s a simple example of this; read it from right to left:

$d = $c = $b = $a = 1;

First we set $a to 1, and the result of this is 1. $b is set with that, the result of which is 1. And so it goes on.

Scoping

All the variables we’ve seen so far in our programs have been global variables. That is, they can be seen and changed from anywhere in the program. For the moment, that’s not too much of a problem, since our programs are very small, and we can easily understand where things get assigned and used. However, when we start writing larger programs, this becomes a problem.

Why is this? Well, suppose one part of your program uses a variable,$counter. If another part of your program wants a counter, it can’t call it$counteras well for fear of clobbering the old value. This becomes more of an issue when we get into subroutines, which are little sections of code we can temporarily call upon to accomplish something for us before returning to what we were previously doing. Currently, we’d have to make sure all the variables in our program had different names, and with a large program that’s not desirable. It would be easier to restrict the life of a variable to a certain area of the program.

To achieve this, Perl provides another type of variable, called lexical variables. These are constrained to the enclosing block and all blocks inside it. If they’re not currently inside a block, they are constrained to the current file. To tell Perl that a variable is lexical, we saymy $variable;. This creates a brand-new lexical variable for the current block, and sets it to the undefined value. Here’s an example:

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

$record = 4;
print "We're at record ", $record, "\n";
{
   my $record;
   $record = 7;
   print "Inside the block, we're at record ", $record, "\n";
}

print "Outside, we're still at record ", $record, "\n";

And this should tell you

$ perl scope1.pl
We're at record 4
Inside the block, we're at record 7
Outside we're still at record 4
$

Let’s look at how this program works. Firstly, we set our global variable$recordto 4.

$record = 4;
print "We're at record ", $record, "\n";

Now we enter a new block, and create a new lexical variable. Important! This is completely and utterly unrelated to the global variable$recordasmy()creates a new lexical variable. This exists for the duration of the block only, and has the undefined value.

{
   my $record;

Next, the lexical variable is set to7, and printed out. The global$recordis unchanged.

   $record = 7;
   print "Inside the block, we're at record ", $record, "\n";

Finally, the block ends, and the lexical copy ends with it. We say that it has gone out of scope. The global remains however, and so$recordhas the value 4.

}

print "Outside, we're still at record ", $record, "\n";

In order to make us think clearly about our programming, we will ask Perl to be strict about our variable use. The statementuse strict;checks that, amongst other things, we’ve declared all our variables. We declare lexicals with themy()function. Here’s what happens if we change our program touse strictformat:

#!/usr/bin/perl -w
# scope2.pl
use strict;

$record = 4;
print "We're at record ", $record, "\n";

{
   my $record;
   $record = 7;
   print "Inside the block, we're at record ", $record, "\n";
}

print "Outside, we're still at record ", $record, "\n";

Now, the global$recordis not declared. So sure enough, Perl complains about it, generating this output:

$ perl scope2.pl
Global symbol "$record" requires explicit package name at scope2.pl line 6.
Global symbol "$record" requires explicit package name at scope2.pl line 7.
Global symbol "$record" requires explicit package name at scope2.pl line 15.
Execution of scope2.pl aborted due to compilation errors.
$

We’ll see exactly what this means in later chapters, but for now it suffices to declare$recordas amy()variable:

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

use strict;

my $record;
$record = 4;
print "We're at record ", $record, "\n";

{
   my $record;
   $record = 7;
   print "Inside the block, we're at record ", $record, "\n";
}

print "Outside, we're still at record ", $record, "\n";

Now Perl is happy, and we get the same output as before. You should almost always start your programs with ause strict. Of course, nobody’s going to force you, but it will help you avoid a lot of mistakes, and will certainly give other people who have to look at your code more confidence in it.



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