我似乎记得 Perl 5 格式仅适用于包变量,并且 PBP似乎在第 449 页上支持了这一点:
Formats rely on global variables for configuration, and on package variables for the data they are to format (see Chapter 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 的某些版本中是否发生了变化,还是我误解了什么?
最佳答案
从 5.001 开始?来自 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.)
关于perl - Perl 5 格式能够处理词法变量多久了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4138318/