Perl中的局部变量和全局变量 [英] Local and Global variables in perl

查看:395
本文介绍了Perl中的局部变量和全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Perl中的local/our范围毫不怀疑.我读了很多文档,但是我仍然感到困惑.以下是困惑

I am having few doubts about the local/our scope in Perl. I read a lot of documentation, but I am still in confusion is there. Following are the confusions

  1. 什么是local范围?

我读的是->本地复制全局变量的值,更改该值,用户将使用它,而在块外它将保留全局值

what I read is -> local copies the value of global variable, change the value, user will use it and outside the block it will retain the global value

混乱-> my做同样的事情.我看到的唯一好处是,某些像$package::var这样的变量不能用我的作用域声明,而可以用局部作用域声明.本地还有什么

Confusion -> my does the same thing. Only benefit I see is that some variables like $package::var cannot be declared with my scope but can be declared with local scope. What else for local

什么是全局"变量?

读取的是->它的作用域在包中.基本上,我们将全局变量放在@EXPORT数组中并使用它,或将名称空间附加到它以在其他包中使用.

What is read is -> Its scope is within the package. Basically we put the global variable in @EXPORT array and use it or append the namespace with it to use in other packages.

doubt->同样,如果仅在main中使用my范围声明变量,则可以在整个包中访问该变量.是对的吗?是否可以在@EXPORT数组中添加my作用域变量并在其他程序包中使用它?

doubt -> Again if we declare variable with my scope in main only then we can access the variable throughout the package. Is that right? Is it possible to add the my scoped variables in @EXPORT array and use it in another packages?

我认为全局变量是用our关键字声明的.还有其他方法吗?

I think global variables are declared with our keyword. Is there any other way to do so?

这个问题可能看起来很重复,但是我很困惑.

This question may look repetitive, but I am confused.

推荐答案

就范围而言,Perl中有两种变量.

In terms of scoping, there are two kinds of variables in Perl.

  • 词法变量是词法范围的,这意味着它们仅在当前词法范围中可见.
  • 包变量是全局范围的,这意味着它们在解释器中的所有代码中都是可见的.

以下是创建变量的方法.

Here are ways to create variable.

  • my创建一个词法变量.
  • our创建一个词法变量,该词法变量的别名为当前包中同名的变量.换句话说,our $foo;alias my $foo = $The::Current::Package::foo;相同.
  • 全局变量是在使用时创建的.
  • my creates a lexical variable.
  • our creates a lexical variable that is aliased to the variable of the same name in the current package. In other words, our $foo; is the same as alias my $foo = $The::Current::Package::foo;.
  • Global variables are created on use.

local不创建任何变量.它只是备份一个变量,直到当前的词法作用域被破坏为止.

local doesn't create any variables. It simply backs up a variable until the current lexical scope is destroyed.

my做同样的事情.

不. local不会更改变量的范围.虽然词法变量仅在词法范围内可见,但局部全局变量在整个解释器中仍然可见.

No. local does not change the scope of a variable. While a lexical variable is only visible in a lexical scope, a localized global variable is still visible across the entire interpreter.

$x = 123;
sub foo { print "$x\n"; }
{ local $x = 456; foo(); }  # 456
foo();                      # 123

$x = 123;
sub foo { print "$x\n"; }
{ my $x = 456; foo(); }   # 123
foo();                    # 123


local

local主要用于近似化my的功能,用于无法以词法声明的变量.

local is primarily used to approximate the functionality of my for variables that cannot otherwise be declared lexically.

(从历史上看,这就是所有变量.从5.6开始,不能按词法声明标点符号变量.)

(Historically, that was all variables. Since 5.6, only punctuation variables cannot be declared lexically.)

什么是全局"变量?

What is "global" variable?

一个可以全局查看的变量,即可以通过解释器中的任何代码查看的变量.

A variable that can seen globally, i.e. by any code in the interpreter.

是否可以在@EXPORT数组中添加我的作用域变量并在其他软件包中使用它?

Is it possible to add the my scoped variables in @EXPORT array and use it in another packages?

不. @EXPORT由导出程序使用.导出程序将无法找到除全局符号以外的任何内容(因为文件是在新的词法范围内编译的),因此@EXPORT必须仅包含全局符号.

No. @EXPORT is used by Exporter. Exporter would not be able to find anything but global symbols (since files are compiled in fresh lexical scopes), so @EXPORT must only contain global symbols.

这篇关于Perl中的局部变量和全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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