Perlcritic - 两个参数“开放”错误

发布于 2024-12-22 00:47:52 字数 280 浏览 2 评论 0原文

我有一个脚本,我正在尝试使用 perlcritic 消除不良做法。

我的一行如下:

open(my($FREESPCHK), $cmdline ) || &zdie($MSG_PASSTHRU,"Error checking free space of file system.");

这给出了这个错误: 在第 xxx 行第 x 列使用了两个参数“open”。参见 PBP 第 207 页。 (严重性:5)

关于如何修复它有什么想法吗?

I have a script and I am trying to elimate bad practices using perlcritic.

One line I have is as follows:

open(my($FREESPCHK), $cmdline ) || &zdie($MSG_PASSTHRU,"Error checking free space of file system.");

This gives this error:
Two-argument "open" used at line xxx, column x. See page 207 of PBP. (Severity: 5)

Any ideas on how to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深已缘浅 2024-12-29 00:47:52

如果您使用 --verbose 11 标志,您将获得更详细的错误解释。在这种情况下,您得到的错误如下所示:

第 6 行使用了两个参数“open”,靠近“open FILE, 'somefile';”。
输入输出::ProhibitTwoArgOpen(严重性:5)

“open”的三参数形式(Perl 5.6 中引入)可防止
当文件名以有趣的字符开头时出现的微妙错误
就像“>”或“<”。 IO::File 模块提供了一个很好的面向对象的
文件句柄的接口,我认为无论如何它更优雅。

 open( $fh, '>output.txt' ); # 不行
 open( $fh, q{>}, 'output.txt' ); # 好的

 使用 IO::文件;
 我的 $fh = IO::File->new( 'output.txt', q{>} ); # 甚至更好!

定义文件的输入模式也更加明确,如下
两者之间的区别:

 open( $fh, 'foo.txt' ); # 不好:读者必须思考默认模式是什么
  open( $fh, '<', 'foo.txt' ); # 好:读者可以看到打开模式

如果文件明确声明它是,则此策略不会抱怨
通过 include 语句与 5.6 之前的 perl 版本兼容,
例如,其中包含“require 5.005”。

我通过阅读 perlcritic 文档找到了这一点。

If you use the --verbose 11 flag, you'll get a far more detailed explanation of the error. In this case, the error you get is looks like this:

Two-argument "open" used at line 6, near 'open FILE, 'somefile';'.
InputOutput::ProhibitTwoArgOpen (Severity: 5)

The three-argument form of `open' (introduced in Perl 5.6) prevents
subtle bugs that occur when the filename starts with funny characters
like '>' or '<'. The IO::File module provides a nice object-oriented
interface to filehandles, which I think is more elegant anyway.

 open( $fh, '>output.txt' );          # not ok
 open( $fh, q{>}, 'output.txt' );     # ok

 use IO::File;
 my $fh = IO::File->new( 'output.txt', q{>} ); # even better!

It's also more explicitly clear to define the input mode of the file, as
in the difference between these two:

  open( $fh, 'foo.txt' );       # BAD: Reader must think what default mode is
  open( $fh, '<', 'foo.txt' );  # GOOD: Reader can see open mode

This policy will not complain if the file explicitly states that it is
compatible with a version of perl prior to 5.6 via an include statement,
e.g. by having `require 5.005' in it.

I found this by reading the perlcritic documentation.

和影子一齐双人舞 2024-12-29 00:47:52

要让 Perl Critic 闭嘴,但没有真正的好处,只需将代码修改为:

open(my $PIPE_FROM_FREESPCHK, "-|", $cmdline)
    || zdie($MSG_PASSTHRU, "Error checking free space of file system.");

但是请注意,从更明显的情况来看,这在任何方面都绝对没有更好

open(my $PIPE_FROM_FREESPCHK, "$cmdline |")
    || zdie($MSG_PASSTHRU, "Error checking free space of file system.");

因为你不是分离出用于直接调用 exec 的令牌。看起来更像是这样:

open(my $PIPE_FROM_FREESPCHK, "-|", $cmd_name, @cmd_args)
    || zdie($MSG_PASSTHRU, "Error checking free space of file system.");

问题是您是否正在运行 shell 命令或只是执行某些操作。如果您的免费支票类似于 df 。 2>/dev/null | 2>/dev/null | awk ....,那么你需要完整的 shell。如果它只是df,那么你就不需要。

To make Perl Critic shut up, but do no real good at all, just modify the code to:

open(my $PIPE_FROM_FREESPCHK, "-|", $cmdline)
    || zdie($MSG_PASSTHRU, "Error checking free space of file system.");

Note however that this is absolutely no better in any regard whatsoever from the far more obvious:

open(my $PIPE_FROM_FREESPCHK, "$cmdline |")
    || zdie($MSG_PASSTHRU, "Error checking free space of file system.");

Because you are not separating out your tokens for calling exec directly. That would look more like this:

open(my $PIPE_FROM_FREESPCHK, "-|", $cmd_name, @cmd_args)
    || zdie($MSG_PASSTHRU, "Error checking free space of file system.");

The question is whether you are running a shell command or just exec’ing something. If your free check is something like df . 2>/dev/null | awk ...., then you need the full shell. If it is just df, then you don’t.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文