home *** CD-ROM | disk | FTP | other *** search
/ c't freeware shareware 1997 / CT_SW_97.ISO / pc / software / entwickl / win95 / pw32i306.exe / lib / strict.pm < prev    next >
Text File  |  1996-10-03  |  3KB  |  116 lines

  1. package strict;
  2.  
  3. =head1 NAME
  4.  
  5. strict - Perl pragma to restrict unsafe constructs
  6.  
  7. =head1 SYNOPSIS
  8.  
  9.     use strict;
  10.  
  11.     use strict "vars";
  12.     use strict "refs";
  13.     use strict "subs";
  14.     use strict "untie";
  15.  
  16.     use strict;
  17.     no strict "vars";
  18.  
  19. =head1 DESCRIPTION
  20.  
  21. If no import list is supplied, all possible restrictions are assumed.
  22. (This is the safest mode to operate in, but is sometimes too strict for
  23. casual programming.)  Currently, there are four possible things to be
  24. strict about:  "subs", "vars", "refs", and "untie".
  25.  
  26. =over 6
  27.  
  28. =item C<strict refs>
  29.  
  30. This generates a runtime error if you 
  31. use symbolic references (see L<perlref>).
  32.  
  33.     use strict 'refs';
  34.     $ref = \$foo;
  35.     print $$ref;    # ok
  36.     $ref = "foo";
  37.     print $$ref;    # runtime error; normally ok
  38.  
  39. =item C<strict vars>
  40.  
  41. This generates a compile-time error if you access a variable that wasn't
  42. localized via C<my()> or wasn't fully qualified.  Because this is to avoid
  43. variable suicide problems and subtle dynamic scoping issues, a merely
  44. local() variable isn't good enough.  See L<perlfunc/my> and
  45. L<perlfunc/local>.
  46.  
  47.     use strict 'vars';
  48.     $X::foo = 1;     # ok, fully qualified
  49.     my $foo = 10;     # ok, my() var
  50.     local $foo = 9;     # blows up
  51.  
  52. The local() generated a compile-time error because you just touched a global
  53. name without fully qualifying it.
  54.  
  55. =item C<strict subs>
  56.  
  57. This disables the poetry optimization, generating a compile-time error if
  58. you try to use a bareword identifier that's not a subroutine, unless it
  59. appears in curly braces or on the left hand side of the "=E<gt>" symbol.
  60.  
  61.  
  62.     use strict 'subs';
  63.     $SIG{PIPE} = Plumber;       # blows up
  64.     $SIG{PIPE} = "Plumber";     # just fine: bareword in curlies always ok
  65.     $SIG{PIPE} = \&Plumber;     # preferred form
  66.  
  67.  
  68.  
  69. =item C<strict untie>
  70.  
  71. This generates a runtime error if any references to the object returned
  72. by C<tie> (or C<tied>) still exist when C<untie> is called. Note that
  73. to get this strict behaviour, the C<use strict 'untie'> statement must
  74. be in the same scope as the C<untie>. See L<perlfunc/tie>,
  75. L<perlfunc/untie>, L<perlfunc/tied> and L<perltie>.
  76.  
  77.     use strict 'untie';
  78.     $a = tie %a, 'SOME_PKG';
  79.     $b = tie %b, 'SOME_PKG';
  80.     $b = 0;
  81.     tie %c, PKG;
  82.     $c = tied %c;
  83.     untie %a ;        # blows up, $a is a valid object reference.
  84.     untie %b;        # ok, $b is not a reference to the object.
  85.     untie %c ;        # blows up, $c is a valid object reference.
  86.  
  87. =back
  88.  
  89. See L<perlmod/Pragmatic Modules>.
  90.  
  91.  
  92. =cut
  93.  
  94. sub bits {
  95.     my $bits = 0;
  96.     foreach $sememe (@_) {
  97.     $bits |= 0x00000002 if $sememe eq 'refs';
  98.     $bits |= 0x00000200 if $sememe eq 'subs';
  99.     $bits |= 0x00000400 if $sememe eq 'vars';
  100.     $bits |= 0x00000800 if $sememe eq 'untie';
  101.     }
  102.     $bits;
  103. }
  104.  
  105. sub import {
  106.     shift;
  107.     $^H |= bits(@_ ? @_ : qw(refs subs vars untie));
  108. }
  109.  
  110. sub unimport {
  111.     shift;
  112.     $^H &= ~ bits(@_ ? @_ : qw(refs subs vars untie));
  113. }
  114.  
  115. 1;
  116.