Question

Pragmas, like autodie, according to the docs, are lexically scoped.

{
use autodie;
  ..
  ..
}
# Can die here

Does this apply to all modules loaded with use? As far as I know, use is almost the same as:

BEGIN {
  require autodie;
  autodie->import(LIST);
}

BEGIN happens at compile time, and require is not lexically scoped. So how is autodie aware of its scope?

Was it helpful?

Solution

The short answer is that lexically-scoped pragmatic modules are explicitly written to behave that way, and use the magical internal variables $^H and %^H during compile time to enable and disable features.

The compiler plays its part by implicitly localising these variables so their value is restored at the end of compiling a code block to what it was at the start. In that way it provides the basis of lexical semantics.

Originally only the $^H variable was available. It contains a bit mask that dictates which compiler options are available at any time during the compilation. Because of that the only lexical pragmas that could be written were ones that manipulated the defined set of magic bits in $^H.

Later the %^H hash was introduced, and any pragma can now store values in this hash with keys that start with the name of the pragma. Because the compiler localises the hash in the same way as the scalar, any pragma can store automatically scoped status information here.

The autodie module doesn't manipulate either of these variables, but subclasses the Fatal module which does all the hard work. It uses %^H to keep track of which operators have been made fatal, and relies on the compiler to discard this information at the end of the block.

OTHER TIPS

From the import method of Fatal.pm which is the backend of autodie, enjoy this:

# Dark magic to have autodie work under 5.8
# Copied from namespace::clean, that copied it from
# autobox, that found it on an ancient scroll written
# in blood.

# This magic bit causes %^H to be lexically scoped.
$^H |= 0x020000;

So the answer is there really is a way to make your imports aware of their lexical scope, but it's deeply entangled with the guts of perl and not meant for ordinary programmers to use.

It's not require that's interesting; it's what the pragma does in import.

Most (all?) pragmas use $^H or %^H. The parser localises these to the scope being parsed, meaning it restores them to the value they had before

Take strict, for example. Its import modifies $^H. $^H contains a series of flags that instructs the compiler how to behave.

$ perl -e'
    BEGIN { printf "%04X\n", $^H }
    {
       use strict;
       BEGIN { printf "%04X\n", $^H }
    }
    BEGIN { printf "%04X\n", $^H }
'
0100
0702
0100

$^H is reserved for Perl's usage, but similarly localised %^H is available for general use. For example, feature::qw_comment hooks into the parser once, when it's loaded by require, but doesn't do anything unless $^H{'feature::qw_comments::'} is true. It's import is equivalent to

sub import { $^H{'feature::qw_comments::'} = 1; }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top