是否有理由在 Perl 中使用 open(...) 的双参数形式? [英] Are there reasons to ever use the two-argument form of open(...) in Perl?

查看:48
本文介绍了是否有理由在 Perl 中使用 open(...) 的双参数形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何理由在 Perl 中使用 open(...) 的两个参数形式而不是三个或更多参数的版本?

Are there any reasons to ever use the two-argument form of open(...) in Perl rather than the three-or-more-argument versions?

我能想到的唯一原因是显而易见的观察结果,即双参数形式更短.但是假设冗长不是问题,是否还有其他原因让您选择 open(...) 的双参数形式?

The only reason I can come up with is the obvious observation that the two-argument form is shorter. But assuming that verbosity is not an issue, are there any other reasons that would make you choose the two-argument form of open(...)?

推荐答案

One-and two-arg open 应用任何由 -C 开关或 open 指定的默认层语用.三 arg 打开没有.在我看来,这种功能差异是选择其中一个的最有力的理由(选择会因您打开的内容而异).哪个是最简单或最具描述性或最安全"的(您可以安全地使用带有任意文件名的两个参数 open,它只是不方便)在模块代码中退居次席;在脚本代码中,您可以更自由地选择是否支持默认图层.

One- and two-arg open applies any default layers specified with the -C switch or open pragma. Three-arg open does not. In my opinion, this functional difference is the strongest reason to choose one or the other (and the choice will vary depending what you are opening). Which is easiest or most descriptive or "safest" (you can safely use two-arg open with arbitrary filenames, it's just not as convenient) take a back seat in module code; in script code you have more discretion to choose whether you will support default layers or not.

此外,Damian Conway 的文件 slurp 运算符需要单参数打开

Also, one-arg open is needed for Damian Conway's file slurp operator

$_ = "filename";
$contents = readline!open(!((*{!$_},$/)=\$_));

这篇关于是否有理由在 Perl 中使用 open(...) 的双参数形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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