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

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

问题描述

我对 Perl 中的 local/our 范围几乎没有怀疑.我阅读了很多文档,但我仍然很困惑.以下是困惑

  1. 什么是local作用域?

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

    Confusion -> my 做同样的事情.我看到的唯一好处是像 $package::var 这样的一些变量不能用我的范围声明,但可以用本地范围声明.本地还有什么

  2. 什么是全局"变量?

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

怀疑 -> 同样,如果我们仅在 main 中声明具有 my 范围的变量,那么我们就可以在整个包中访问该变量.是对的吗?是否可以在 @EXPORT 数组中添加 my 作用域变量并在其他包中使用它?

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

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

解决方案

在作用域方面,Perl 中有两种变量.

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

以下是创建变量的方法.

  • my 创建一个词法变量.
  • our 创建一个词法变量,它是当前包中同名变量的别名.换句话说,our $foo;alias my $foo = $The::Current::Package::foo; 是一样的.
  • 全局变量是在使用时创建的.

local 不创建任何变量.它只是简单地备份一个变量,直到当前的词法作用域被销毁.

<小时><块引用>

my 做同样的事情.

没有.local 不会改变变量的作用域.虽然词法变量只在词法范围内可见,但局部化的全局变量在整个解释器中仍然可见.

$x = 123;子 foo { 打印$x
";}{ 本地 $x = 456;富();#456富();#123

$x = 123;子 foo { 打印$x
";}{ 我的 $x = 456;富();#123富();#123

<小时><块引用>

还有什么local

local 主要用于逼近 my 的功能,用于无法在词法上声明的变量.

(过去,这是所有变量.从 5.6 开始,只有标点变量不能在词法上声明.)

<小时><块引用>

什么是全局"变量?

全局可见的变量,即解释器中的任何代码.

<小时><块引用>

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

没有.@EXPORT 被导出器使用.导出器除了全局符号之外什么都找不到(因为文件是在新的词法范围内编译的),所以 @EXPORT 必须只包含全局符号.

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. What is local scope?

    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

    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

  2. What is "global" variable?

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 -> 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?

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.

解决方案

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

  • Lexical variables are lexically scoped, which means they are only visible in the current lexical scope.
  • Package variables are globally scoped, which means they are visible by all code in the interpreter.

Here are ways to create variable.

  • 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 doesn't create any variables. It simply backs up a variable until the current lexical scope is destroyed.


my does the same thing.

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
"; }
{ local $x = 456; foo(); }  # 456
foo();                      # 123

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


What else for local

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

(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.


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

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天全站免登陆