in reply to Import pragmas like strict and warnings into callers lexical scope

All strict->import (normally called via use strict) does is modify global var $^H. The compiler uses $^H to determine if it should throw strict errors, among other things.

The compiler localises $^H to the block being compiled. This is what produces the lexical scoping of the pragma.

{ # $^H is localised by the compiler # when it compiles this. use strict; # Changes $^H ... } # $^H is restored by the compiler # when it compiles this, # undoing changes in strictures.

In your example, the block being compiled when strict->import is called is your script's file block, so the strictures stay in effect until the end of the file.

Similarly scoped hash %^H is available to modules that want to be lexical pragmas without having to fight Perl for the limited bits in $^H.

Update: Added code example. Added mention of %^H.

Replies are listed 'Best First'.
Re^2: Import pragmas like strict and warnings into callers lexical scope
by youwin (Beadle) on Feb 11, 2011 at 22:35 UTC
    Is it special that Perl restores $^H at the end of the enclosing block? Like could I write an import statement that does the same with a different global variable ($main::G for instance)?
      It's special to $^H and %^H, but %^H was created to give you access to that behaviour
      $^H{'mypragma'} = ...;
      $^H{'mypragma::G'} = ...;