http://qs321.pair.com?node_id=11107261

Lotus1 has asked for the wisdom of the Perl Monks concerning the following question:

I'm running Perl 5.24.1 x86 on a Windows 10 (Edit) Server 2012 R2 system. My question is why are there no errors when I open a filename that contains a colon ( ':' ) character? What happens is the open works and printing to the filehandle seems to work but the file that is created has a truncated name and no output is actually put into that file. The filename is truncated at the colon character.

The reason I'm asking is that before this I trusted the open to tell me if there was a problem with creating an output file. I was trying to test my logic in a larger program and put ':' in the filename to trigger an open error and it did not appear. The program silently failed with a garbled filename for the logfile.

Testcase number 8 in this program shows the problem. All the other tests either work or produce a file open error.

use warnings; use strict; my $count = 1; my @test_characters = qw( * . " / \ [ ] : ; | ); push @test_characters, ' '; push @test_characters, ','; foreach my $char (@test_characters){ print "char = \'$char\'\n"; my $logfile = $0; $logfile =~ s/\.pl$/_$count-$char---\.log/; $count++; print "logfile = >$logfile<\n"; if(not open my $fh_log, ">", $logfile) { print "Error *** Couldn't open logfile for output: $logfile - +$! : $^E ---\n",'-'x79,"\n"; next; } else { print "-- Opened logfile for output: $logfile ---\n"; if( not print $fh_log "-- Opened logfile for output: $logfile +---\n",'-'x79,"\n" ){ print "Couldn't print to file handle.$!--\n"; } print '-'x79,"\n"; } }

This is what is printed to the display:

char = '*' logfile = >testlog_1-*---.log< Error *** Couldn't open logfile for output: testlog_1-*---.log - Invalid argument : The filename, directory name, or volume label syntax is incorrect -- +- ---------------------------------------------------------------------- +--------- char = '.' logfile = >testlog_2-.---.log< -- Opened logfile for output: testlog_2-.---.log --- ---------------------------------------------------------------------- +--------- char = '"' logfile = >testlog_3-"---.log< Error *** Couldn't open logfile for output: testlog_3-"---.log - Invalid argument : The filename, directory name, or volume label syntax is incorrect -- +- ---------------------------------------------------------------------- +--------- char = '/' logfile = >testlog_4-/---.log< Error *** Couldn't open logfile for output: testlog_4-/---.log - No such file or directory : The system cannot find the path specified --- ---------------------------------------------------------------------- +--------- char = '\' logfile = >testlog_5-\---.log< Error *** Couldn't open logfile for output: testlog_5-\---.log - No such file or directory : The system cannot find the path specified --- ---------------------------------------------------------------------- +--------- char = '[' logfile = >testlog_6-[---.log< -- Opened logfile for output: testlog_6-[---.log --- ---------------------------------------------------------------------- +--------- char = ']' logfile = >testlog_7-]---.log< -- Opened logfile for output: testlog_7-]---.log --- ---------------------------------------------------------------------- +--------- char = ':' logfile = >testlog_8-:---.log< -- Opened logfile for output: testlog_8-:---.log --- ---------------------------------------------------------------------- +--------- char = ';' logfile = >testlog_9-;---.log< -- Opened logfile for output: testlog_9-;---.log --- ---------------------------------------------------------------------- +--------- char = '|' logfile = >testlog_10-|---.log< Error *** Couldn't open logfile for output: testlog_10-|---.log - Invalid argument : The filename, directory name, or volume label syntax is incorrect -- +- ---------------------------------------------------------------------- +--------- char = ' ' logfile = >testlog_11- ---.log< -- Opened logfile for output: testlog_11- ---.log --- ---------------------------------------------------------------------- +--------- char = ',' logfile = >testlog_12-,---.log< -- Opened logfile for output: testlog_12-,---.log --- ---------------------------------------------------------------------- +---------

Here is the contents of the folder that shows the truncated filename 'testlog_8-'

Volume in drive D is APPS Volume Serial Number is XXX Directory of D:\scripts\clear_dmp_files\test 10/09/2019 03:28 PM <DIR> . 10/09/2019 03:28 PM <DIR> .. 10/09/2019 03:28 PM 0 testdir.txt 10/09/2019 03:01 PM 833 testlog.pl 10/09/2019 03:27 PM 136 testlog_11- ---.log 10/09/2019 03:27 PM 136 testlog_12-,---.log 10/09/2019 03:27 PM 135 testlog_2-.---.log 10/09/2019 03:27 PM 135 testlog_6-[---.log 10/09/2019 03:27 PM 135 testlog_7-]---.log 10/09/2019 03:27 PM 0 testlog_8- 10/09/2019 03:27 PM 135 testlog_9-;---.log 10/09/2019 03:27 PM 2,629 testlog_out.txt 10 File(s) 4,274 bytes 2 Dir(s) 129,251,168,256 bytes free