Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

However, occasionally there is a need to disable warnings or strictness for some code that may look strange , but is actually correct. The -w switch cannot enable or disable particular warnings on particular ranges of code. When a particular warning or strict checking must be disabled, the no warnings or no strict pragmas should be used in as minimal a scope as possible. They should also disable the specific warning or strictness checker that would trigger a warning or fatal error , rather than disable all checks.

...

Code Block
bgColor#ffcccc
langperl

use warnings;
use strict;

my %days = ("Sunday"    => 'pray',
            "Monday"    => 'work',
            "Tuesday"   => 'work',
            "Wednesday" => 'work',
            "Thursday"  => 'work',
            "Friday"    => 'work',
            "Saturday"  => 'rest');

sub what_to_do {
  my $day = shift;
  if ($days{$day} eq 'work') {
    return 'work hard';
  }
  if (exists $days{$day}) {
    return $days{$day};
  } else {
    return "do nothing";
  }
}


my $task = what_to_do('tomorrow');
print "Prepare to $task\n";

This code produces the output:

Code Block

Use of uninitialized value within %days in string eq at ./example.pl line 16.
Prepare to do nothing

...

Code Block
bgColor#ffcccc
langperl

use warnings;
use strict;
no warnings 'uninitialized';

my %days = ("Sunday"    => 'pray',
# ...

Unfortunately while , although this does code correctly suppress suppresses the warning message, it has the undesired effect of suppressing the warning message throughout the entire program . This has the likely effect of suppressing and will likely suppress the warning in other lines of code that are not known to be correct.

...

Code Block
bgColor#ccccff
langperl

sub what_to_do {
  my $day = shift;
  no warnings 'uninitialized';
  if ($days{$day} eq 'work') {
    return 'work hard';
  }
  if (exists $days{$day}) {
    return $days{$day};
  } else {
    return "do nothing";
  }
}

...

Code Block
bgColor#ffcccc
langperl

use strict;
use warnings;

our $sunday    = 'pray';
our $monday    = 'work';
our $tuesday   = 'work';
our $wednesday = 'work';
our $thursday  = 'work';
our $friday    = 'work';
our $saturday  = 'rest';

sub what_to_do {
  my $day = shift;
  no warnings 'uninitialized';
  if ($$day eq 'work') {
    return 'work hard';
  }
  if (defined $$day) {
    return $$day;
  } else {
    return "do nothing";
  }
}


my $task = what_to_do('tomorrow');
print "Prepare to $task\n";

The strict pragma catches the improper reference and aborts the program, producing the following error message:

Code Block

Can't use string ("tomorrow") as a SCALAR ref while "strict refs" in use at ./example.pl line 19.

...

This noncompliant code example disables the strict pragma, thus producing proper output. However, strict-ness strictness is suppressed throughout the entire program.

Code Block
bgColor#ffcccc
langperl

use warnings;
use strict;

no strict 'refs';

our $sunday    = 'pray';
# ...

This code produces the output:

Code Block

Prepare to do nothing

This example may be considered correct, but the code works by referencing a nonexistent variable $tomorrow.

...

This compliant solution suppresses the strict-ness strictness checking to as minimal a scope as possible. Because the strict checking is suppressed only inside the what_to_do subroutine, other regions of the code can still be checked for strict compliance.

Code Block
bgColor#ccccff
langperl

sub what_to_do {
  my $day = shift;
  no warnings 'uninitialized';
  no strict 'refs';
  if ($$day eq 'work') {
    return 'work hard';
  }
  if (defined $$day) {
    return $$day;
  } else {
    return "do nothing";
  }
}

...