#! /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符号表中做什么是不可打印的字符呢?他们的符号是什么?
解决方法
“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等).