What's the difference between “local” and “my” in Perl

There are two kinds of variable scopes in Perl:

  • Global variables: They reside in the current package, can be accessed from the outside and can have "local" values. The name can be used as a key in the "stash", the package variable hash / the symbol table.
  • Lexical variables: They reside in the current scope (roughly delimited by curly braces). There is no symbol table that can be inspected.

Lexical variables and global variables do not interfere, there can be two different variables with the same name.

Most Perl variable magic happens with global variables: These lines are rougly equivalent:

our $var;
$::var;
$main::var;
${var};
${'var'};
local $var;

but not my $var.

So we can write:

@::array = qw(a b c);
my @secondArray = @{array};

Which copies the arrays. We can also look up the array with a name that is stored in a variable:

@::array = qw(a b c);
my $name = "array";
my @secondArray = @{$name};

The last line abbreviates to … = @$name.

This is not possible with lexical vars because they do not reside in the stash.

The local function assigns a "local" value to a global variable (and globals only) within the current scope and in the scope of all subs that are called from within this scope.

Originally (in Perl 4) meddling with variable names and the stash was the only way to simulate references. These usages are now mostly outdated by ~2 decades as references are available (what is far safer)


There is a perldoc entry which answers this question in perlfaq7:

What's the difference between dynamic and lexical (static) scoping? Between local()and my()?

local($x) saves away the old value of the global variable $x and assigns a new value for the duration of the subroutine which is visible in other functions called from that subroutine. This is done at run-time, so is called dynamic scoping. local() always affects global variables, also called package variables or dynamic variables. my($x) creates a new variable that is only visible in the current subroutine. This is done at compile-time, so it is called lexical or static scoping. my() always affects private variables, also called lexical variables or (improperly) static(ly scoped) variables.

For instance:

sub visible {
    print "var has value $var\n";
}

sub dynamic {
    local $var = 'local';   # new temporary value for the still-global
    visible();              #   variable called $var
}

sub lexical {
    my $var = 'private';    # new private variable, $var
    visible();              # (invisible outside of sub scope)
}

$var = 'global';
visible();              # prints global
dynamic();              # prints local
lexical();              # prints global

Notice how at no point does the value "private" get printed. That's because $var only has that value within the block of the lexical() function, and it is hidden from the called subroutine.

In summary, local() doesn't make what you think of as private, local variables. It gives a global variable a temporary value. my() is what you're looking for if you want private variables.

See Private Variables via my() in perlsub and Temporary Values via local() in perlsub for excruciating details.

.
9/2/2012 Update 
This weekend, I reread the book of Effective Programming Perl, and found more full answer in the section: Item 43 Understand the difference between my and local.  

Reference: http://stackoverflow.com/questions/12189865/whats-the-difference-between-local-and-my-in-perl#


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值