在 Perl 中,local() 可以创建一个变量吗? [英] In Perl, can local() create a variable?

查看:61
本文介绍了在 Perl 中,local() 可以创建一个变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Stackoverflow 和 Google 中阅读了许多帖子,这些帖子告诉我们 local 不创建变量,而是对现有变量起作用.

I have read many posts in Stackoverflow and in Google which tell that local does not create a variable, instead it works on the existing ones.

我在下面有一小段代码,我想知道 local 在没有创建这样的变量时是如何工作的.

I have a small piece of code below and I wonder how local is working when there is no such variable already created.

#use strict;
#use warnings;

&func;

sub func{
    local $temp = 20;
    print $temp;        
}

我写这个只是为了理解这个概念,我对 Perl 比较陌生.

This I wrote just to understand the concept and I am relatively new to Perl.

推荐答案

除非您使用 my 声明变量,否则没有完整包规范的变量会进入当前包.以下是您可能会如何看到首次使用的变量以及它们将是什么:

Unless you declare a variable with my, variables without a full package specification go into the current package. Here's how you might see variables used for the first time and what they would be:

my $temp;       # a scoped, lexical variable that does not live in any package
state $temp;    # a persistent lexical variable
our $temp;      # a package variable in the current package, declared
$temp;          # a package variable in the current package
$main::temp     # a package variable in main
$Foo::Bar::temp # a package variable in Foo::Bar
local $temp     # a package variable in the current package, with a dynamically-scoped (temporary) value

local 设置包变量的范围.当你声明这个动态"范围时,Perl 使用你设置的临时值直到范围结束.与其他包变量一样,Perl 在您第一次使用它们时创建它们.您可能首先使用它并在前面使用 local 不会影响这一点.

The local sets the scope of a package variable. When you declare this "dynamic" scope, Perl uses the temporary value you set until the end of the scope. As with other package variables, Perl creates them when you first use them. That you might use it first with local in front doesn't affect that.

许多试图回答您问题的人立即对您唠叨strict.这是一种编程辅助工具,可通过强制您声明要使用的所有变量来帮助您不输入错误的变量名称.当您使用未声明的变量名称时,它会停止编译您的程序.您可以使用 vars pragma、mystateour:

Many people who tried to answer your question immediately nagged you about strict. This is a programming aid that helps you not mistype a variable name by forcing you to declare all variables you intend to use. When you use a variable name you haven't declared, it stops the compilation of your program. You can do that with the vars pragma, my, state, or our:

use vars qw($temp);
our $temp;
my $temp;
state $temp;

正如您所见,

local 不是其中的一部分.为什么?因为事情就是这样.如果不同,我会更喜欢它.

local isn't part of that, as you've seen. Why? Because that's just how it is. I'd like it more if it were different.

strict 不会抱怨.您可能会在不知不觉中打错所有这些内容.

strict won't complain if you use the full package specification, such as $Foo::Bar::temp. You can mistype all of those without ever noticing.

我主要保留将 local 用于 Perl 的特殊变量,您不必声明这些变量.如果我想在子程序中使用 $_,也许要使用默认使用 $_ 的运算符,我可能会从 local $_ 开始:

I mostly reserve my use of local for Perl's special variables, which you don't have to declare. If I want to use $_ in a subroutine, perhaps to use the operators that use $_ by default, I'll probably start that with local $_:

 sub something {
     local $_ = shift @_;
     s/.../.../;
     tr/.../.../;
     ...;
     }

我可能更频繁地将 local 与输入记录分隔符一起使用,这样我就可以使用不同的行结尾而不会影响之前出现的:

I probably use local more often with the input record separator so I can use different line endings without affecting might have come before:

 my $data = do { local $/; <FILE> };

这些之所以有效,是因为隐式首次使用了您未见过的变量.

Those work because there's an implicit first use of those variables that you haven't seen.

否则,我可能想让它的子程序私有变量,这样子程序之外的任何东西都看不到它.在这种情况下,我不想要程序的其余部分可以读取或写入的包变量.这就是 my 变量的工作:

Otherwise, I probably want to make variables private to its subroutine so nothing outside the subroutine can see it. In that case, I don't want a package variable that the rest of the program can read or write. That's the job for my variables:

sub something {
    my $temp = ...;

    }

编程的诀窍是将可能发生的事情限制在您想要的范围内.如果程序的其余部分不应该能够看到或更改变量,my 就是要走的路.

The trick of programming is to limit what can happen to exactly what you want. If the rest of your program shouldn't be able to see or change the variable, my is the way to go.

我解释这是学习Perl并在掌握 Perl.

这篇关于在 Perl 中,local() 可以创建一个变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆