Unlike many other programming languages, it is not necessary to declare variables before use in Perl. Declaring a variable with a certain name simply means stating to the Perl interpreter that the program needs a variable with the given name. It enables the programming system to perform housekeeping such as allocating the appropriate amount of space for the variable. A variable is declared before we actually use the variable for the first time. In Perl, if a variable is not declared before its use, it just springs into existence as soon as it is used in the program in a statement that provides it with a value. If an undeclared and unassigned variable is used for the first time, it assumes a default value such as 0, the empty string, or the empty list, depending on its type and the context of usage.
Declaration of variables helps the Perl allocate space and makes Perl run a bit faster. It also helps write programs that are easier to debug. Perl can be instructed to complain if we use a variable name that has not been declared before its use. This means that if we make an error in spelling the name of a variable inside the program, Perl catches the error and lets us know. It is considered a good programming practice to declare any variable used in the program. Perl does not enforce pre-declaration of variables. It leaves the choice to the programmer. If a programmer wants the benefits that accrue from declaration of variables, he or she may choose to enforce this discipline on himself or herself. This is consistent with a fundamental belief of Perl’s creator that as few rules as possible should be enforced when writing software to stifle creativity. However, many academics and practitioners disagree with such a position arguing that program maintainability is a casualty.
Perl has several ways for declaring variables. The most common way is by using the my declaration. It is sufficient for most simple programs. A variable declared with my is a so-called lexically scoped variable in that it is available only within the block in which it is declared. For simple programs such as the ones we have seen so far, the whole file is assumed to belong to one block. In the following program which is a rewrite of the immediately preceding program, the scope of each variable is the whole program. A block is usually contained inside curly brackets, { and }. We will see examples of such blocks in the next section.
Even when declaration for variable names is enforced, unlike many languages, Perl does not require that all declarations be made at the top of the block, although it is a good practice. In such situations, Perl is happy as long as the variable is declared anywhere inside the block, but before its first use.
Program 1.3
#!/usr/bin/perl #file scalars-strict.pl use strict vars; my $day; my $date; #Several `my' declared variables can be put in a list also. my ($month, $year, $space, $you, $me); my ($high, $low, $tomorrow); $day = "Wednesday"; $date = 28; $month = 'May'; $year = "1997"; $space = " "; $you = "justin"; $me = 'jugal'; $high = 65.2; $low = 40.3; print "Dear \u$you,\n\n"; print $space x 5; print "How are you doing? "; print "Today is $day, $month $date, $year.\n"; print "Today's high and low temperature were $high and $low degrees F. "; $tomorrow = $date + 1; print "Tomorrow is $month $tomorrow, $year.\n"; print "\n\u$me\n";
In this program, each variable has been declared before usage by using my. We can declare each variable in a statement by itself. We can also declare several variables in one statement. If we declare several variables in one statement, the list of variables must be included inside parentheses.In the program given above, we have made it mandatory that every variable used be pre-declared using my. We have ensured this by using a pragma or Perl directive at the top of the program or the block in question:
use strict;
A directive requires Perl to look out for things as asked. If we use any variable in this program that is not declared a priori using my, Perl will give an error. The pragma could have been written also as:
use strict vars;
This is because use strict; looks for several things, only one of which is ensuring declaration of variable names. The word vars can be quoted also.