Perl Programming Scalars: Building a Currency Converter |
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 $record = 4; print "Outside, we're still at record ", $record, "\n"; And this should tell you $ perl scope1.pl Let’s look at how this program works. Firstly, we set our global variable$recordto 4. $record = 4; 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. { Next, the lexical variable is set to7, and printed out. The global$recordis unchanged. $record = 7; 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 $record = 4; { 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 We’ll see exactly what this means in later chapters, but for now it suffices to declare$recordas amy()variable: #!/usr/bin/perl -w use strict; my $record; { 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.
blog comments powered by Disqus |
|
|
|
|
|
|
|