X hits on this document

PDF document

Running and Debugging Perl - page 7 / 30

82 views

0 shares

0 downloads

0 comments

7 / 30

Running and Debugging Perl

Aha! A warning in the 'once' category has been fired, telling us that we've only used the variable tutal once. Obviously, we've misspelled total here.

That's enough to help us track down and fix the problem, but what about the other error: $tutal certainly had an uninitialized value, but where is the concatenation? We didn't use the . operator – however, perl did. Internally, perl understands "something $a" to be "something ".$a. Since the $a in this case was undefined, perl complained.

The Scope of use warnings

The warnings pragma is lexically scoped, so its effects will last throughout the same block of code as a my variable would – that is, within the nearest enclosing braces or the current file. For instance, the following program has warnings throughout:

#!/usr/bin/perl

  • #

    warntest3.plx

use warnings;

{

my @a = qw(one , two , three , four); } my @b = qw(one , two , three , four);

Therefore perl responds with the following warnings, in the qw category:

>perl warntest3.plx Possible attempt to separate words with commas at warntest3.plx line 6. Possible attempt to separate words with commas at warntest3.plx line 8. >

reminding us that since qw() automatically changes separate words into separate elements, we don't need to separate them with commas.

If you really do want commas as some elements of your array, you may turn warnings off by saying no warnings. In the following program, warnings are only turned on for the code outside the brackets:

#!/usr/bin/perl

  • #

    warntest4.plx

use warnings;

{

no warnings; my @a = qw(one , two , three , four); } my @b = qw(one , two , three , four);

Now perl will only give the one warning, for the second array:

  • >

    perl warntest4.plx

Possible attempt to separate words with commas at warntest3.plx line 9. >

285

Document info
Document views82
Page views82
Page last viewedWed Dec 07 17:12:36 UTC 2016
Pages30
Paragraphs723
Words10014

Comments