Perl 内部变量.下一个表达式是否相同? [英] Perl internal variables. Are next expressions same?

查看:44
本文介绍了Perl 内部变量.下一个表达式是否相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表达:

${ 'main::' }{ "_<$filename" }

${ "::_<$filename" }

这两个表达式一样吗?

推荐答案

否,但以下内容是等效的:

No, but the following are equivalent:

${"_<$filename"}         # When this expression is found in package main
${"::_<$filename"}
${"main::_<$filename"}

${ $::{"_<$filename"} }
${ ${'::'}{"_<$filename"} }

${ $main::{"_<$filename"} }
${ ${'main::'}{"_<$filename"} }

这些是指根/主命名空间中的变量,其名称由 "_<$filename" 生成.

These are refer to the variable in the root/main namespace whose name is produced by "_<$filename".

举个更清楚的例子,以下都是等价的(假设前两个代码是在Foo::Bar包中编译的):

As a clearer example, the following are all equivalent (assuming the code is compiled in package Foo::Bar for the first two):

@moo                                  # Via hardcoded, unqualified name
@{'moo'}                              # Via symref using unqualified name

@Foo::Bar::moo                        # Via hardcoded, qualified name
@{'Foo::Bar::moo'}                    # Via symref using qualified name

@{ *Foo::Bar::moo }                   # Via hardcoded glob
@{ *{'Foo::Bar::moo'} }               # Via symref to glob

@{ $Foo::Bar::{moo} }                 # Via glob via hardcoded package
@{ ${'Foo::Bar::'}{moo} }             # Via glob via symref to package

@{ $Foo::{'Bar::'}{moo} }             # Via glob via hardcoded parent package
@{ ${'Foo'}::{'Bar::'}{moo} }         # Via glob via symref to parent package

@{ $::{'Foo::'}{'Bar::'}{moo} }       # Via glob via root package
@{ $main::{'Foo::'}{'Bar::'}{moo} }   # Same

<小时>

Perl 允许使用符号名称作为引用.


Perl allows symbol names to be used as references.

$ perl -e'%h = ( a => 4, b => 5 ); my $ref = "h"; CORE::say $ref->{a};'
4

这些被称为符号引用"(并且在使用 use strict; 时是不允许的).以下使用 main:: 作为符号引用:

These are called "symbolic references" (and they're not allowed when using use strict;). The following uses main:: as a symbolic reference:

${ 'main::' }{ "_<$filename" }

然而,这样做没有意义.由于变量的名称无论如何都是硬编码的,我们不妨使用以下内容:

There's no point to doing that, however. Since the name of the variable is hardcoded anyway, we might as well use the following:

$main::{ "_<$filename" }

这不仅更简单,在使用use strict;时也是允许的.

Not only is this simpler, it's allowed when using use strict;.

但是什么是%main::?嗯,这就是 main 包的符号表.%main:: 的键是 main 包中存在的符号(变量)的名称(没有任何符号).

But what is %main::? Well, that's the symbol table for the package main. The keys of %main:: are the names of the symbols (variables) that exist in the package main (without any sigil).

%main:: 的值是符号表条目.我们称这些为typeglobs",或简称为globs".取消引用一个 glob 就好像它是一个引用一样访问适合取消引用的类型的变量.例如,

The values of %main:: are symbol table entries. We call these "typeglobs", or "globs" for short. Dereferencing a glob as if it was a reference access the variable of the type appropriate for the dereference. For example,

$main::{a}

返回

*main::a       # A glob

${ $main::{a} }

返回

$main::a

这意味着下面的代码返回一个 glob 到 main 命名空间中的变量,其名称由 "_<$filename"

This means that the following returns a glob to the variable in the main namespace whose name is produced by "_<$filename"

$main::{ "_<$filename" }

<小时>

主命名空间也是根命名空间.这意味着


The main namespace is also the root namespace. This means that

$main::a

$::a

两者都指向同一个变量.这也意味着

both refer to the same variable. It also means that

%main::

%::

两者都指向同一个变量.这也意味着

both refer to the same variable. It also means that

$main::{ "_<$filename" }

$::{ "_<$filename" }

两者都返回相同的 glob.

both return the same glob.

我们已经确定,下面的代码会返回一个指向根/主命名空间中名称由 "_<$filename"

We've established that the following returns a glob to the symbol in the root/main namespace whose name is produced by "_<$filename"

$::{ "_<$filename" }

我们还确定以下引用根目录/主目录中的标量,其名称由 "_<$filename" 生成:

We've also established that the following references the scalar in the root/main whose name is produced by "_<$filename":

${ $::{ "_<$filename" } }

最后,我们还确定以下是引用相同标量的符号引用:

And finally, we've also established that following is a symbolic reference that references the same scalar:

${ "::_<$filename" }

这篇关于Perl 内部变量.下一个表达式是否相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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