为什么这个关于strict subs的错误只会在autodie下触发? [英] Why does this error about strict subs only trigger under autodie?

查看:31
本文介绍了为什么这个关于strict subs的错误只会在autodie下触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定这样的代码,我不会收到任何警告.

使用严格;使用警告;打开 STDERR, '>&', STDOUT;

给定这段代码,我得到一个致命错误.

使用严格;使用警告;使用autodie;打开 STDERR, '>&', STDOUT;

<块引用>

在 ./test.pl 中使用strict subs"时不允许使用裸词STDOUT"第 6 行./test.pl 的执行由于编译错误而中止.

为什么 autodie 将严格的 subs 指向这个错误的根源——在第一个例子中显然是严格的,我没有错误.

使用 diagnostics 错误解释如下..

<块引用>

在 ./test.pl 第 7 行使用strict subs"时不允许使用裸词STDOUT"../test.pl 的执行由于编译错误而中止 (#1) (F) 使用strict subs"时,只允许在大括号中或=>"左侧使用裸字作为子程序标识符象征.也许您需要预先声明一个子程序?

这一切都可以通过做来解决

打开 STDERR, '>&', *STDOUT;

那么它不是一个裸字,但是为什么裸字特殊情况下是 open without autodie,而不是 autodie?这里还有其他事情吗?

解决方案

autodie 通过导出名为 open 的子项来实现其任务,Perl 使用该子项来支持 open 运算符.

$ perl -MO=Concise,-exec -e' open(my $fh, "<", "foo")'...8 <@>打开[t3] vK/3...$ perl -MO=Concise,-exec -e'use autodie;open(my $fh, "<", "foo")'...7 <#>gv[*open] s8 <1>进入sub vKS...

open 操作符有特殊的解析规则,不能被原型复制[1],所以 open 操作符可以't 被 sub 准确复制.这解释了观察到的差异.

<小时>

  1. 通常情况下,prototype("CORE::opname") 会为此类运算符返回 undefined,但 prototype("CORE::open") 不准确地报告 open 的解析规则等价于 *;$@ 原型.

Given code like this, I get no warning whatsoever.

use strict;
use warnings;
open STDERR, '>&', STDOUT;

Given this code, I get a fatal error.

use strict;
use warnings;
use autodie;
open STDERR, '>&', STDOUT;

Bareword "STDOUT" not allowed while "strict subs" in use at ./test.pl line 6. Execution of ./test.pl aborted due to compilation errors.

Why is autodie pointing at strict subs as being the source of this error -- when clearly strict was on in the first example and I had no error.

With diagnostics the error is explained like this..

Bareword "STDOUT" not allowed while "strict subs" in use at ./test.pl line 7. Execution of ./test.pl aborted due to compilation errors (#1) (F) With "strict subs" in use, a bareword is only allowed as a subroutine identifier, in curly brackets or to the left of the "=>" symbol. Perhaps you need to predeclare a subroutine?

This can all be resolved by doing

open STDERR, '>&', *STDOUT;

Then it's not a bareword, but why is the bareword special cased for open without autodie, and not with autodie? Is something else happening here?

解决方案

autodie achieves its task by exporting a sub named open that Perl uses in favour of the open operator.

$ perl -MO=Concise,-exec -e'             open(my $fh, "<", "foo")'
...
8  <@> open[t3] vK/3
...

$ perl -MO=Concise,-exec -e'use autodie; open(my $fh, "<", "foo")'
...
7  <#> gv[*open] s
8  <1> entersub vKS
...

The open operator has special parsing rules that can't be replicated by a prototype[1], so the open operator can't be accurately replicated by a sub. This accounts for the observed difference.


  1. Normally, prototype("CORE::opname") returns undefined for such operators, but prototype("CORE::open") inaccurately reports that open's parsing rules are equivalent to the *;$@ prototype.

这篇关于为什么这个关于strict subs的错误只会在autodie下触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆