X hits on this document

PDF document

Running and Debugging Perl - page 10 / 30

90 views

0 shares

0 downloads

0 comments

10 / 30

Chapter 9

Even if it goes right, there's no guarantee that $section won't contain a name you're using somewhere else in the program. A variable name you're using may be blown away at any moment by something outside your program. This isn't a pretty situation to get into, and use strict stops you from getting into it by disallowing the use of symbolic references.

Strict on Subroutines

Last, but not least, use strict disallows 'poetry optimization', which lets you use barewords as strings. This means if you want to use the name of a subroutine without brackets, you must declare the subroutine first. For example:

#!/usr/bin/perl

  • #

    strictsubs1.plx

use warnings; use strict;

$a = twelve; sub twelve { return 12 }

blows up with an error:

>perl strictsubs1.plx Bareword "twelve" not allowed while "strict subs" in use at strictsubs1.plx line 6 Execution of strictsubs1.plx aborted due to compilation errors. >

However, this is okay. You'll get the query 'Name "main::a" used only once: possible typo' but that's simply because we've declared $a and then not used it. We'll come back to this error in a minute:

#!/usr/bin/perl

  • #

    strictsubs2.plx

use warnings; use strict;

sub twelve { return 12 } $a = twelve;

Of course, you can always get round the limitation on barewords, simply by not using them. A subroutine name with parentheses is always OK:

#!/usr/bin/perl

  • #

    strictsubs3.plx

use warnings; use strict;

sub twelve { return 12 } $a = twelve();

These three areas – variables, symbolic references and subroutines – are split into categories just like the warnings. These are vars, refs, and subs respectively.

As before, use strict turns on all checks. You can turn on and off all or individual checks on a lexical basis just as you could with use warnings:

288

Document info
Document views90
Page views90
Page last viewedSat Dec 10 15:14:45 UTC 2016
Pages30
Paragraphs723
Words10014

Comments