Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Perl's two arg open seem to strip newlines?

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.

like image 855
pilcrow Avatar asked Jun 28 '12 21:06

pilcrow


2 Answers

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.

like image 65
Sinan Ünür Avatar answered Sep 24 '22 16:09

Sinan Ünür


It's quite simple: Because 2-arg open's syntax approximates the shell's, even to the point of allowing pipes.

like image 22
ikegami Avatar answered Sep 26 '22 16:09

ikegami