Hide Forgot
Description of problem: Open() fails if file name ends in a space Version-Release number of selected component (if applicable): 5.22.2 How reproducible: 100% Steps to Reproduce: 1.$file = 't '; 2.$o = open($fh, $file)|| die 'whoops'; 3. Actual results: "whoops" Expected results: no "whoops" Additional info:
Checking what the code does on operating level shows: $ strace -e open ./test open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3 open("/lib64/libperl.so.5.22", O_RDONLY|O_CLOEXEC) = 3 open("/lib64/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3 open("/lib64/libresolv.so.2", O_RDONLY|O_CLOEXEC) = 3 open("/lib64/libnsl.so.1", O_RDONLY|O_CLOEXEC) = 3 open("/lib64/libdl.so.2", O_RDONLY|O_CLOEXEC) = 3 open("/lib64/libm.so.6", O_RDONLY|O_CLOEXEC) = 3 open("/lib64/libcrypt.so.1", O_RDONLY|O_CLOEXEC) = 3 open("/lib64/libutil.so.1", O_RDONLY|O_CLOEXEC) = 3 open("/lib64/libc.so.6", O_RDONLY|O_CLOEXEC) = 3 open("/lib64/libfreebl3.so", O_RDONLY|O_CLOEXEC) = 3 open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3 open("/dev/urandom", O_RDONLY) = 3 open("./test", O_RDONLY) = 3 open("/usr/share/perl5/strict.pm", O_RDONLY) = 4 open("/usr/share/perl5/warnings.pm", O_RDONLY) = 4 open("t", O_RDONLY) = -1 ENOENT (No such file or directory) whoops: No such file or directory at ./test line 5. +++ exited with 2 +++ So the problem is that the trailing white space is removed. Reading open() documentation in perlfunc POD revelads this is a feature: The filename passed to the one- and two-argument forms of open() will have leading and trailing whitespace deleted and normal redirection characters honored. This property, known as "magic open", can often be used to good effect. A user could specify a filename of "rsh cat file |", or you could change certain filenames as needed: $filename =~ s/(.*\.gz)\s*$/gzip -dc < $1|/; open(FH, $filename) or die "Can't open $filename: $!"; Use the three-argument form to open a file with arbitrary weird characters in it, open(FOO, "<", $file) || die "can't open < $file: $!"; otherwise it's necessary to protect any leading and trailing whitespace: $file =~ s#^(\s)#./$1#; open(FOO, "< $file\0") || die "open failed: $!"; Therefore I recommend you to use 3-argument form with explicit open mode like this: $o = open($fh, '<', $file)|| die 'whoops';