Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How does strict work?

by epoptai (Curate)
on Feb 17, 2001 at 09:36 UTC ( #59089=perlquestion: print w/ replies, xml ) Need Help??
epoptai has asked for the wisdom of the Perl Monks concerning the following question:

Here is the strict.pm source code:
$strict::VERSION = "1.01"; my %bitmask = ( refs => 0x00000002, subs => 0x00000200, vars => 0x00000400 ); sub bits { my $bits = 0; foreach my $s (@_){ $bits |= $bitmask{$s} || 0; }; $bits; } sub import { shift; $^H |= bits(@_ ? @_ : qw(refs subs vars)); } sub unimport { shift; $^H &= ~ bits(@_ ? @_ : qw(refs subs vars)); } 1;

Comment on How does strict work?
Download Code
Re: How does strict work?
by merlyn (Sage) on Feb 17, 2001 at 09:44 UTC
Re: How does strict work?
by IndyZ (Friar) on Feb 17, 2001 at 09:51 UTC
    This does seem rather voodoo-ish at first, until we read the documentation. "perldoc perlvar" reveals that $^H holds, "the current set of syntax checks enabled by use strict and other block scoped compiler hints". It would seem to me that all of the actual strict code is built into the perl binary, and strict.pm uses tricks like bitmasks to set certain bits in this variable, telling perl to turn on/off certain parts of the strict checking code. You could set the bits yourself, if you wanted to live semi-dangerously.

    --
    IndyZ

Re (tilly) 1: How does strict work?
by tilly (Archbishop) on Feb 17, 2001 at 09:53 UTC
    If you check in perlvar you will find out that $^H is an internal and highly magical variable. It is just a collection of flags with rather special scoping semantics. What strict does is flip the appropriate bits to what they need to be to cause certain things to (not) be checked when you want them to (not) be.

    If it helps, I am willing to treat it as black magic and hope that the phase of the moon is right... :-)

    UPDATE
    What I suspect (verifying it would take some research) is happening is that $^H is a global variable which the parser is auto-localizing as it goes through blocks. So within strict you write to the global, but within the parser the effect of that modification is scoped to just the current block. Therefore the part of the user code that is affected is apparently lexically scoped.

    A couple of other pieces of magic though. First of all when you see an eval, Perl remembers what the correct value of $^H needs to be when it is called. Secondly within a file it needs to essentially do local $^H = $^H; while when it goes out and compiles another file it would have to do local $^H = 0;.

    So the magic isn't all that magic, it just looks mysterious because of when it is happening. If you want to test this explanation, each of the following tests another aspect of its behaviour:

    perl -e 'print $^H' perl -e 'use strict; print $^H' perl -e 'use strict; no strict; print $^H' perl -e 'use strict; {no strict; print $^H}' perl -e 'use strict; BEGIN {no strict; print $^H}' perl -e 'use strict; {no strict; BEGIN{ print $^H}}'
    (People on windows may need to switch quotes.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://59089]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-12-29 00:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls