Perl 5 格式能够处理词法变量多久了?

发布于 2024-10-01 14:25:41 字数 489 浏览 0 评论 0原文

我似乎记得 Perl 5 格式仅适用于包变量,并且 PBP 似乎在页面上支持了这一点449:

格式依赖于全局变量进行配置,并依赖于包变量来获取要格式化的数据(请参阅第 5 章)。

但快速测试似乎表明情况并非如此(使用 5.8.9、5.10.0 和 5.12.1 进行测试):

#!/usr/bin/perl

use strict;
use warnings;

my $foo = "hello world";
write;

format STDOUT =
@<<<<<<<<<<<<<<<
$foo
.

这是否在 Perl 5 的某些版本中发生了变化,或者我误解了什么?

I seem to remember that Perl 5 formats only worked with package variables, and PBP seems to back that up on page 449:

Formats rely on global variables for configuration, and on package variables for the data they are to format (see Chapter 5).

But a quick test seems to show otherwise (tested with 5.8.9, 5.10.0, and 5.12.1):

#!/usr/bin/perl

use strict;
use warnings;

my $foo = "hello world";
write;

format STDOUT =
@<<<<<<<<<<<<<<<
$foo
.

Did this change in some version of Perl 5, or am I misunderstanding something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈下烟灰 2024-10-08 14:25:41

从 5.001 开始?来自 perlform

词法变量(用“my”声明)在格式中不可见,除非该格式是在词法变量的范围内声明的。 (在版本 5.001 之前它们根本不可见。)

Since 5.001? From perlform:

Lexical variables (declared with "my") are not visible within a format unless the format is declared within the scope of the lexical variable. (They weren't visible at all before version 5.001.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文