Monday, March 05, 2012

Warnings, variables, and unnested brackets

I have long been fascinated by improperly nested bracketed constructs, such as [ ( ] ).

Today I ran into an example of a situation that might warrant such improper nesting. I decided to clean up a Perl script, converting it to 'use strict'.  Along the way I enabled warnings using Perl's lexically scoped 'use warnings' facility. In places I had to disable warnings, to make the code compile and run with few enough changes.

And so I encountered:

if( ... ) {
   ...
   my $m = an expression with an ignorable warning;
   ...
}
I want to disable the warning for the smallest possible region, but
if( ... ) {
   ...
   { no warnings 'type';
   my $m = an expression with an ignorable warning;
   }
   ...
}
restricts the scope of both the warning disable (good), but also the variable (nad). Whereas letting the warning be disabled until the end of the enclosing lexical scope
if( ... ) {
   ...
   no warnings 'type';
   my $m = an expression with an ignorable warning;
   ...
}
disables the warning for too large a region. What you want is
if( ... ) {
   ...
   <disable-warnings 'type'>
   <variable-scope 'm'>
   my $m = an expression with an ignorable warning;
   </disable-warnings>
   ...
   </variable-scope>
}