perl - Perl 符号表中不可打印的字符代表什么?

标签 perl

我刚刚了解到,在 Perl 中,给定模块的符号表存储在与模块名称匹配的哈希中——例如,虚构模块的符号表 Foo::Bar将是 %Foo::Bar .默认符号表存储在 %main:: .出于好奇,我决定想看看 %main:: 中的内容。 ,因此遍历哈希中的每个键/值对,并在我进行时将它们打印出来:

#! /usr/bin/perl

use v5.14;
use strict;
use warnings;

my $foo;
my $bar;
my %hash;

while( my ( $key, $value ) = each  %:: )  {
    say "Key: '$key' Value '$value'";
} 

输出如下所示:
Key: 'version::' Value '*main::version::'
Key: '/' Value '*main::/'
Key: '' Value '*main::'
Key: 'stderr' Value '*main::stderr'
Key: '_<perl.c' Value '*main::_<perl.c'
Key: ',' Value '*main::,'
Key: '2' Value '*main::2'
...

我期待看到 STDOUT 和 STDERR 文件句柄,也许还有 @INC 和 %ENV ......我没想到看到的是非 ascii 字符......上面的代码块没有显示的是输出的第三行实际上有一个表示不可打印字符的字形。

我运行脚本并按如下方式通过管道传输它:
perl /tmp/asdf.pl | grep '[^[:print:]]' | while read line
do 
    echo $line
    od -c <<< $line
    echo
done

输出如下所示:
Key: '' Value '*main::'
0000000   K   e   y   :       ' 026   '       V   a   l   u   e       '
0000020   *   m   a   i   n   :   : 026   '  \n
0000032

Key: 'ARNING_BITS' Value '*main::ARNING_BITS'
0000000   K   e   y   :       ' 027   A   R   N   I   N   G   _   B   I
0000020   T   S   '       V   a   l   u   e       '   *   m   a   i   n
0000040   :   : 027   A   R   N   I   N   G   _   B   I   T   S   '  \n
0000060

Key: '' Value '*main::'
0000000   K   e   y   :       ' 022   '       V   a   l   u   e       '
0000020   *   m   a   i   n   :   : 022   '  \n
0000032

Key: 'E_TRIE_MAXBUF' Value '*main::E_TRIE_MAXBUF'
0000000   K   e   y   :       ' 022   E   _   T   R   I   E   _   M   A
0000020   X   B   U   F   '       V   a   l   u   e       '   *   m   a
0000040   i   n   :   : 022   E   _   T   R   I   E   _   M   A   X   B
0000060   U   F   '  \n
0000064

Key: ' Value '*main:'
0000000   K   e   y   :       '  \b   '       V   a   l   u   e       '
0000020   *   m   a   i   n   :   :  \b   '  \n
0000032

Key: '' Value '*main::'
0000000   K   e   y   :       ' 030   '       V   a   l   u   e       '
0000020   *   m   a   i   n   :   : 030   '  \n
0000032

那么 Perl 符号表中的不可打印字符在做什么呢?它们的符号是什么?

最佳答案

大师走在正确的轨道上:具体来说,答案可以在 perlvar 中找到。 ,其中说:

"Perl variable names may also be a sequence of digits or a single punctuation or control character. These names are all reserved for special uses by Perl; for example, the all-digits names are used to hold data captured by backreferences after a regular expression match. Perl has a special syntax for the single-control-character names: It understands ^X (caret X) to mean the control-X character. For example, the notation $^W (dollar-sign caret W) is the scalar variable whose name is the single character control-W. This is better than typing a literal control-W into your program.

Since Perl 5.6, Perl variable names may be alphanumeric strings that begin with control characters (or better yet, a caret). These variables must be written in the form ${^Foo}; the braces are not optional. ${^Foo} denotes the scalar variable whose name is a control-F followed by two o's. These variables are reserved for future special uses by Perl, except for the ones that begin with ^_ (control-underscore or caret-underscore). No control-character name that begins with ^_ will acquire a special meaning in any future version of Perl; such names may therefore be used safely in programs. $^_ itself, however, is reserved."


如果您想以可读的方式打印这些名称,您可以在代码中添加这样的一行:
$key = '^' . ($key ^ '@') if $key =~ /^[\0-\x1f]/;
如果 $key 的第一个字符是一个控制字符,这将用一个插入符号替换它,后跟相应的字母(^A 用于控制-A,^B 用于控制-B,等等)。

关于perl - Perl 符号表中不可打印的字符代表什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15917167/

相关文章:

windows - Strawberry Perl 是 %ENV 一个普通的对象

linux - 在 perl 中过滤具有给定输入范围的文件

regex - Perl 正则表达式跳过每一行

regex - 根据 perl 数组评估输入的邮政编码

perl - 从复杂的 Perl 数据结构中删除空数组引用和单例数组引用

multithreading - Perl线程: Why does the output come by order and not mixed-up?

perl - 如何在 Perl 中编写缓存 HTTP 代理?

perl - HTTP::Tiny 类似 FTP 处理

perl - 保存 Perl Windows 环境 key UPCASES

perl - xml::Twig 和 findnodes