Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bracket code section in use strict / no strict?

Tags:

perl

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?

like image 721
stevesliva Avatar asked Mar 04 '15 23:03

stevesliva


1 Answers

"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
}
like image 189
ysth Avatar answered Sep 28 '22 10:09

ysth