在 Perl 6 中导出和绑定 [英] is export and binding in Perl 6

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

问题描述

为什么没有导出带有 := 绑定的变量的值?

Why isn't the value of a variable with := binding exported?

$ cat myModule.pm6 
our $a is export = 42;
our $b is export := $a;

$ cat program.p6 
use myModule;
say $a;
say $b;

$ perl6 program.p6 
42
(Any)   # Why?

推荐答案

our 范围的变量实际上只是一个词法变量(如 my),它 - 而不是为每个范围新创建一个 Scalar - 通过绑定到当前包的 Stash 中该名称的符号来初始化.如此有效,这:

An our-scoped variable is really just a lexical variable (like my) that - instead of having a Scalar freshly created per scope - is initialized by being bound to a symbol of that name in the Stash of the current package. So effectively, this:

our $foo;

正在这样做:

my $foo := $?PACKAGE.WHO<$foo>;

所以:

our $foo = 42;

正在这样做:

(my $foo := $?PACKAGE.WHO<$foo>) = 42;

因此重新绑定符号意味着它不再与存储在 Stash 中的 Scalar 容器相关联.

Rebinding the symbol thus means it's no longer associated with the Scalar container stored in the Stash.

导出 our 范围的变量会从变量在范围进入时绑定到的存储中导出 Scalar 容器.因此,分配分配到导出的 Scalar 容器中.相比之下,绑定用完全不同且与导出的内容无关的东西替换了词法.

Exporting an our-scoped variable exports the Scalar container from the stash that the variable is bound to at scope entry time. Thus the assignment assigns into that exported Scalar container. By contrast, binding replaces the lexical with something entirely different and unrelated to what was exported.

这就是为什么不允许导出 my 范围的变量:每个范围条目都绑定了一个新的 Scalar,但导出是编译时的事情,因此无法修改导出的内容.

This is why you aren't allowed to export a my-scoped variable: a fresh Scalar is bound every scope entry, but exportation is a compile-time thing, so there would be no way to ever modify the thing that was exported.

这篇关于在 Perl 6 中导出和绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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