为什么Perl的两个arg打开似乎剥离换行符? [英] Why does Perl's two arg open seem to strip newlines?

查看:126
本文介绍了为什么Perl的两个arg打开似乎剥离换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用perl的two-arg open()时,包含一个尾随换行符的文件名将被忽略。然而,三参数版本保留了换行符。



为什么行为不同?为什么表面上剥离了新行?



- 文件nl不存在。让它



 $ ls -1 nl;触摸nl; ls -1 nl 
ls:nl:没有这样的文件或目录
nl



- 尝试三个arg opennl \ n→ ENOENT



strace显示我期望的行为,FWIW。

 $ perl -E'open F,<,nl \\\
)或者$!'
在-e行1没有这样的文件或目录。

$ strace -e trace = open perl -E'打开(F,<,nl \ n)或者死亡$!'2>&1 | grep nl
open(nl\\\
,O_RDONLY | O_LARGEFILE)= -1 ENOENT(无此文件或目录)



- 现在,试试两个arg打开nl \ n→成功吗?



 $ perl -E'open(F,nl\\\
)or die $!'



- 什么?为什么这样做?让我们来看看strace。
$ b $ p

哦,它忽略了换行符:

 $ strace -e trace =打开perl -E'打开(F,nl \ n)或者死亡$!'2>&1 | grep nl 
open(nl,O_RDONLY | O_LARGEFILE)= 3



- nl仍然是唯一的文件



 $ ls 
nl

背景:

 $ perl -v 

这是为i686构建的perl 5,版本16,subversion 0(v5.16.0) -linux-thread-multi。

解决方案

perldoc perlopentut


另外需要注意的是,在shell中,忽略文件名之前或之后的任何空格。这是好的,因为你不希望这些做不同的事情:



 打开信息,< datafile
打开INFO,< datafile
打开INFO,< datafile




这不是一个错误,而是一个功能。由于打开模仿使用重定向箭头指定如何打开文件的风格的外壳程序,它也是如此关于文件名本身的额外空白也是如此。如需访问具有顽皮名称的文件,请参见消除Dweomer

还有一个3参数版本的open,它可以让你把特殊的重定向字符放到他们自己的参数中:

… p>

在这种情况下,要打开的文件名是 $ datafile 中的实际字符串,所以您不必担心包含可能会影响打开模式的字符的 $ datafile ,或者是文件名开头的空格,这些空格将被吸收到双参数版本中。此外,减少不必要的字符串插值是一件好事。



When I call perl's two-arg open() with a filename containing a trailing newline, the newline is ignored. The three-arg version preserves the newline, however.

Why does the behavior differ? Why is the newline ostensibly stripped out?

-- The file "nl" does not exist. Make it

$ ls -1 nl; touch nl; ls -1 nl
ls: nl: No such file or directory
nl

-- Try to three-arg open "nl\n" → ENOENT

strace shows the behavior I expect, FWIW.

$ perl -E 'open(F, "<", "nl\n") or die $!'
No such file or directory at -e line 1.

$ strace -e trace=open perl -E 'open(F, "<", "nl\n") or die $!' 2>&1 | grep nl
open("nl\n", O_RDONLY|O_LARGEFILE)      = -1 ENOENT (No such file or directory)

-- Now, try the two-arg open "nl\n" → success ?

$ perl -E 'open(F, "nl\n") or die $!'

-- What? Why did that work? Let's look at strace.

Oh, it is ignoring the newline:

$ strace -e trace=open perl -E 'open(F, "nl\n") or die $!' 2>&1 | grep nl
open("nl", O_RDONLY|O_LARGEFILE)        = 3

-- "nl" is still the only file there

$ ls 
nl

Background:

$ perl -v

This is perl 5, version 16, subversion 0 (v5.16.0) built for i686-linux-thread-multi.

解决方案

perldoc perlopentut:

The other important thing to notice is that, just as in the shell, any whitespace before or after the filename is ignored. This is good, because you wouldn't want these to do different things:

open INFO, "<datafile"
open INFO, "< datafile"
open INFO, "< datafile"

This is not a bug, but a feature. Because open mimics the shell in its style of using redirection arrows to specify how to open the file, it also does so with respect to extra whitespace around the filename itself as well. For accessing files with naughty names, see Dispelling the Dweomer.

There is also a 3-argument version of open, which lets you put the special redirection characters into their own argument:

In this case, the filename to open is the actual string in $datafile, so you don't have to worry about $datafile containing characters that might influence the open mode, or whitespace at the beginning of the filename that would be absorbed in the 2-argument version. Also, any reduction of unnecessary string interpolation is a good thing.

这篇关于为什么Perl的两个arg打开似乎剥离换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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