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.