Showing posts with label cleaner code. Show all posts
Showing posts with label cleaner code. Show all posts

Monday, September 13, 2010

Avoiding mistakes in perl

You can control perl's level of "strictness" using command line switches and pragmas

the -w switch

#!/usr/local/bin/perl -w
# deliberate_mistake.pl


$variable = 5; $varaible++;

print "new value = $variable\n";


>>>deliberate_mistake.pl
Name "main::varaible" used only once: possible typo at deliberate_mistake.pl line 5.
new value = 5

the strict pragma

This forces all variables to be explicitly declared using the my keyword.

#!/usr/local/bin/perl -w
# deliberate_mistake2.pl

use strict; # forces all variables to be declared

my $variable = 5; $varaible = $variable + 1;

print "new value = $variable\n";

Notice this would not get caught by the -w switch

>>> deliberate_mistake2.pl
Global symbol "$varaible" requires explicit package name at deliberate_mistake2.pl line 7.
Execution of lectures/1_intro/deliberate_mistake2.pl aborted due to compilation errors.