Search code examples
perlstrict

Bracket code section in use strict / no strict?


I inherited some perl code that of course doesn't use either strict or warnings, and I keep using uninitialized variables and the like.

I'd like to bracket the sections of code that I'm modifying like this:

use warnings;
use strict;

... my code changes and additions ...

no strict;
no warnings;

And that seems to work, but I'm having issues deciphering what the perldoc on use means when it says these are compiler directives that import into the current "block scope." Does that mean that any scope can have a use strict unpaired with a no strict? Is the no strict at the tail of the global scope essentially undoing the meaning of use strict earlier in the same scope?


Solution

  • "block scope" means both use strict; and no strict; have effect from where they are to the end of the innermost enclosing block, so no, a later no strict doesn't undo an earlier use strict. It just changes it for the innermost scope from that point in the code on. So:

    {
        use strict;
        # strict in effect
        {
            # strict still in effect
            no strict;
            # strict not in effect
        }
        # strict in effect
        no strict;
        # strict not in effect
        use strict;
        # strict in effect
    }