6

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?

stevesliva
  • 5,351
  • 1
  • 16
  • 39
  • 1
    I'll confirm this is a good idea to *temporarily* work with existing code. Even better would be to put your additions into functions in a separate library file. You'll eventually want to bring it all under strict. – Schwern Mar 04 '15 at 23:44
  • Someone who doesn't `use strict` of course doesn't also allow easy code encapsulation... I have to add to their eleventytwo global hashtables, you see... – stevesliva Mar 04 '15 at 23:47
  • 3
    Yes, it's frustrating, but it doesn't prevent you from using library subroutines. You can refer to their globals with `%main::blah` (assuming it's in package main). Even better, pass their globals you need to modify into your subroutines by reference: `some_new_sub(\%blah)`. It will prevent their global mess from infecting new code. This will be messy at first, but it will give you a better idea of what code needs what data. Through all this, the Extract Method pattern will be your strongest tool. – Schwern Mar 04 '15 at 23:52

1 Answers1

11

"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
}
ysth
  • 96,171
  • 6
  • 121
  • 214
  • Good illustration, thanks. I had two concerns: 1.) There are few examples of people using `no strict` for this, to bracket changes to unstrict code. 2.) I was worried there was a sort of 2-pass interpretation of scope that might make things not work like illustrated, so the confirmation is good. – stevesliva Mar 04 '15 at 23:41
  • 2
    "Block scope" is also referred to as "lexical scope". It's important to note that lexical scope does *not* enter subroutine calls. "Lexical" as in "what you read on the page". `no strict` in small blocks is often used on the rare occasions when you want to use symbolic references. A common use is creating subroutines on the fly. `{ no strict 'refs'; *{$sub_name} = sub { ... } }`. – Schwern Mar 04 '15 at 23:41
  • 1
    as opposed to "dynamic scope" which does enter subroutine calls – ysth Mar 05 '15 at 00:25