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?
$ ls -1 nl; touch nl; ls -1 nl ls: nl: No such file or directory nl
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)
$ perl -E 'open(F, "nl\n") or die $!'
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
$ 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.
It's quite simple: Because 2-arg open
's syntax approximates the shell's, even to the point of allowing pipes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With