my (Perl function)

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Revision as of 18:27, 26 May 2006 by Woozle (talk | contribs) (My (Perl function) moved to my (Perl function))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The built-in function my() in Perl creates a variable that is local to the current scope. That is, it is defined:

  • within the current set of curly braces {}
  • within any further curly braces that are within that current set (as one would expect)

If a variable of the same name exists outside of the current scope, my() creates a new variable and ignores/preserves the older variable. Query: is there a way to access the older variable from inside this scope? The older variable is once again accessible when execution leaves the current scope.

my() is very similar to local(), except that my() does not do dynamic scoping. my() is generally recommended over local(). (You may hear the saying "Don't use local()" from experienced Perl programmers.)

Versions

  • my() was introduced in Perl 5.

Examples

  • my($avar,$anothervar);
  • my($arg1,$arg2) = @_;
  • my(%hashvar);
  • my %anotherhash;
  • my $justonevar;
  • my @anArray;