Using Dumper not triggering a failure

你离开我真会死。 提交于 2019-12-19 04:18:29

问题


when running code like this:

use strict;
print Dumper "something";

nothing is printed out and no error occurs during compile and runtime. Why does this happen? Why doesn't strict prevent this code from running? Why is there no error at runtime, even though Dumper is unknown?

I know it produces a warning when those are explicitly enabled, but I'm interested why is this code considered "correct" in any way.


回答1:


One of the valid syntaxes for print is

print FILEHANDLE LIST

In your program Perl is treating Dumper as a filehandle glob.

Running this code with warnings enabled will tell you:

print() on unopened filehandle Dumper at ...




回答2:


If you had begun with the standard boilerplate, then you would know:

#!/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;

The answer is that your program is syntactically but not semantically correct. You are printing "something" to the unopened Dumper filehandle-object, because Dumper is in the dative slot for the print method call. That makes Dumper print’s invocant. But you never opened a handle by that name, so you are printing to an uninitialized filehandle.

Use my boilerplate. PLEASE!



来源:https://stackoverflow.com/questions/4272816/using-dumper-not-triggering-a-failure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!