vir*_*tor 2 perl strict data-dumper
当运行这样的代码时:
use strict;
print Dumper "something";
Run Code Online (Sandbox Code Playgroud)
没有打印出来,并且在编译和运行时没有发生错误.为什么会这样?为什么不strict阻止此代码运行?为什么运行时没有错误,即使Dumper未知?
我知道它会在显式启用时产生警告,但我很感兴趣为什么这段代码在任何方面都被认为是"正确的".
tch*_*ist 11
如果您已经开始使用标准样板,那么您会知道:
#!/usr/bin/env perl
#
# name_of_program - what the program does as brief one-liner
#
# Your Name <your_email@your_host.TLA>
# Date program written/released
#################################################################
use 5.10.0;
use utf8;
use strict;
use autodie;
use warnings FATAL => "all";
# ? change to agree with your input: ?
use open ":std" => IN => ":encoding(ISO-8859-1)",
OUT => ":utf8";
# ? change for your output: ? — *maybe*, but leaving as UTF-8 is sometimes better
END {close STDOUT}
our $VERSION = 1.0;
$| = 1;
Run Code Online (Sandbox Code Playgroud)
答案是你的程序在语法上但在语义上不正确.您正在打印"something"到未打开的Dumper文件句柄对象,因为Dumper它位于print方法调用的dative槽中.那Dumper print令人生气.但是你从未打开过该名称的句柄,所以你要打印到未初始化的文件句柄.
使用我的样板. 请!
其中一个有效的语法print是
print FILEHANDLE LIST
Run Code Online (Sandbox Code Playgroud)
在您的程序中,Perl将其Dumper视为文件句柄glob.
在启用警告的情况下运行此代码将告诉您:
print() on unopened filehandle Dumper at ...
| 归档时间: |
|
| 查看次数: |
1086 次 |
| 最近记录: |