sonia hamilton – life on the digital bikepath – sonia@snowfrog.net

22 May 2009

Perl – my, use vars, our, local, use strict

Filed under: Perl — Sonia @ 18:16

Some notes for me as I attempt to get a better understanding of Perl’s variable scoping rules. A work in progress…

  • Perl has 2 separate ‘namespaces’ for variables – package aka global (Perl 4) and my aka private aka lexical scoping (Perl 5)
  • the default package is main, this can be changed with the statement package foo; which then applies to all subsequent code until another package statement is encountered
  • lexical scoping is done with {} (either via subroutines/operators or ‘naked blocks’); the top level is the file itself (‘file level’).
  • use strict; (specifically use strict ‘vars’;) requires that variables either use a package qualifier (eg $main::foo) if they’re package variables or be lexical variables declared with my. You can get around this extra typing for package variables with use vars (or no strict ‘vars’ for a block)
  • our($foo,$bar) is like use vars qw($foo $bar), however it’s lexical ie can be restricted by {}’s (an equivalent no use vars doesn’t exist). Looked at it another way, our ’spans’ package declarations (as it’s lexical) whereas use vars is ‘reset’ by package declarations.

Also:

  • dynamic scoping is done with local – see Wikipedia – Scope (programming). Lexical scoping is the norm in modern programming languages, dynamic scoping is a Lisp’y idea that uses a sort of stack for variable scope resolution, that can only be determined at runtime.  When to use local? Rarely… Seven Useful Uses of local
  • local only works with package variables not lexical/my variables; an example of where local can be useful is to temporarily change a Global Special Variable (eg $) $EFFECTIVE_GROUP_ID) before calling a subroutine: “local saves away the value of a package global and substitutes a new value for all code within and called from the block in which the local declaration is made” eg
{
    local $) = getgrnam(USER) if $< == 0;
    enable($host);
}
  • do C-style static variables in this manner:
        { my $seed = 1;
          sub my_rand {
            $seed = int(($seed * 1103515245 + 12345) / 65536) % 32768;
            return $seed;
          }
        }
  • xx

Some Links:

1 Comment »

  1. Perldoc for strict is also insightful.

    http://perldoc.perl.org/strict.html

    Its considered best practice to always have use strict and use warnings. I believe in perl 6 they will just be part of the language.

    Comment by Dean Hamstead — 25 May 2009 @ 08:30


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.