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 @R_301_156@ 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等).

原文链接:https://www.f2er.com/Perl/171678.html

猜你在找的Perl相关文章