When I call perl two-arg open()
with a file name containing a trailing newline, the newline is ignored. However, the version with three arguments saves the new line.
Why is the behavior different? Why is the anchor out of order?
- The file "nl" does not exist. Do it
$ ls -1 nl; touch nl; ls -1 nl
ls: nl: No such file or directory
nl
- Try tri-arg to open "nl \ n" -> ENOENT
strace shows the behavior that 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 two-arg open "nl \ n" โ success?
$ perl -E 'open (F, "nl \ n") or die $!'
- What? Why did it work? Look at strace.
Oh, he ignores the new line:
$ 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.
source share