The 'strict' pragma has three points of function:

  1. variable declaration (compile-time)
  2. bareword catching (compile-time)
  3. symbolic reference catching (run-time)

The first of these is most often the bane of programmers new to 'strict'.
Saying

  use strict 'vars';

means that, to the end of this scope (block or file), or until a

  no strict 'vars';

is encountered, any variable used must have been declared, or must have an
explicit package name.  Variables can be declared in a few ways:

  1. my $foo;          # a lexical (non-package related) variable
  2. our $foo;         # like my() for package variables
  3. use vars '$foo';  # says $foo is valid in this package

You can give a variable an explicit package name, too:

  1. $main::foo;  # $foo in package main::
  2. $::foo;      # same thing
  3. $Bar::foo;   # $foo in package Bar::


The bareword-catching is handled by

  use strict 'subs';

and has the same scope as 'vars'.  It requires that all barewords be
known filehandles, declared functions, preceded by a -, inside braces,
followed by a =>, or preceded by a << (as in a here-doc).

  1. open FOO, "< file";
  2. sub foo { scalar reverse $_[0] }  $x = foo "hi there";
  3. $opt = -ok_action;
  4. print $people{japhy};
  5. %people = ( japhy => "Marillion" );
  6. print <<END;

Any other use of a bareword (which is an bare identifier[1]) is illegal
under strict 'subs'.


The symbolic reference catching is run-time, and is enabled via

  use strict 'vars';

and has the same scoping laws as the others.  It stops you from doing

  $name = 'japhy';
  $var = 'name';
  print $$var;      # $$var => $name => japhy

You must use hard references:

  $name = 'japhy';
  $var = \$name;
  print $$var;


If you just say

  use strict;

all three options are enabled.  You can turn on multiple options in one
"use" statement.  To turn them off, use "no" instead of "use".

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk?  http://www.perlmonks.com/     http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc.     http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter.         Brother #734
**      Manning Publications, Co, is publishing my Perl Regex book      **


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to