Robert's Perl Tutorial

http://www.sthomas.net/roberts-perl-tutorial.htm


use strict;

So what's strict and how do you use it? The module strict restricts 'unsafe constructs', according to the perldocs. The strict module is a pragma, which is a hint that must be obeyed. Like when your girlfriend says 'oh, that ring is *far* too expensive'.

There is no need to be frightened about unsafe code if you don't mind endless hours of debugging unstructured programs. When you enable the strict module, the three things that Perl becomes strict about are:

This tutorial doesn't presently cover references (and let's hope I remember to remove this sentence if I do cover it in later versions) so we won't worry about refs.

Strict variables are useful. Essentially, this means that all variables must be declared, that is defined before use rather than springing into existence as required. Furthermore, each variable must be defined with my or fully qualified. This is an example of a program that is not strict, and should be executed something like this:

perl script.pl "Alain James Smith";

where the "" enclose the string as a single parameter as opposed to three separate space-delimited parameters.

#use strict;			# uncomment after running a couple of times

$name=shift;			# shifts @ARGV if no arguments supplied

print "The name is $name\n";
$inis=&initials($name);

$luck=int(rand(10)) if $inis=~/^(?:[a-d]|[n-p]|[x-z])/i;

print "The initials are $inis, lucky number: $luck\n";

sub initials {
        my $name=shift;
        $initials.=$1 while $name=~/(\w)\w+\s?/g;
        return $initials;
}

By now you should be able to work out what the above does. When you uncomment the use strict; pragma, and re-run the program, you will get output something like this:

Global symbol "$name" requires explicit package name at n1.pl line 3.
Global symbol "$inis" requires explicit package name at n1.pl line 6.
Global symbol "$luck" requires explicit package name at n1.pl line 8.
Global symbol "$initials" requires explicit package name at n1.pl line 14.
Execution of n1.pl aborted due to compilation errors.

These warnings mean Perl is not exactly clear about what the scope of variables is. If Perl is not clear, you might not be either. So you need to be explicit about your variables, which means either declaring them with my so they are restricted to the current block, or referring to them with their fully qualified name. An example, using both methods:

use strict;

$MAIN::name=shift;			# shifts @ARGV if no arguments supplied

print "The name is ",$MAIN::name,"\n";
my $inis='';
my $luck='';

$inis=&initials($MAIN::name);

$luck=int(rand(10)) if $inis=~/^(?:[a-d]|[n-p]|[x-z])/i;

print "The initials are $inis, lucky number: $luck\n";

sub initials {
        my $name=shift;
	my $initials;
        $initials.=$1 while $name=~/(\w)\w+\s?/g;
        return $initials;
}

The my variables in the subroutine are nothing new. The my variables outside the subroutine are. If you think about it, the main program itself is also a kind of block, and therefore variables can be lexically scoped to be visible only within the block.

The other interesting bit is the $MAIN::name business. This, as you might expect, is the fully qualified name of the variable. The first part is the package name, in this case MAIN. The second part is the actual variable name. Personally, I've never needed to refer to a variable this way. I'm not saying you'll never use the syntax, but I would suggest that knowing this is not on a perl students Top 10 list of Things to Master.

The important thing about use strict is that it does enforce more discipline than you have been used to, and for all but the smallest of programs, that is most definitely a Good Thing.