You asked Windows to create a file, and gave it a name. Windows said, "OK, no problem!", and you got a successful return from open, but behind the scenes, Windows munged the filename. That's why you don't get an error, and Perl probably can't be reasonably expected to catch this sort of thing, as it runs on over a hundred different platforms, and does not guarantee filename in = filename out.

The colon is of course the volume separator on Win32, so while it's not a valid Win32 filename character, it is a valid path character supplied to open, (just as you might open C:\Windows\Crash). It of course isn't doing what you expect, and then I don't see how a "volume" of testlog_8- makes any sense. Chopping off the : and anything that follows, and saving a file with what would be the volume name makes even less sense, but it is what it is, I guess. DOS is, well, a bit different. :-)

Win32 naming is, well, a bit convoluted. This MSDN naming guide article illustrates the rules.

I'm not sure why you'd end up with random characters in your output logfile when you are generating the filename, but the solution is probably one of either a) making sure your filename generator function doesn't use those characters, or b) filtering out invalid characters before the call to open (or erroring out on invalid characters, perhaps).

If you need cross-platform support, it's a little trickier, but you also just be extra-picky and allow alnum, underscore, and dash, for example. I'm not aware of a module that portably processes filenames, otherwise that's what I'd recommend. In practice, a simple regex usually does the trick: $filename =~ s/[^\w-]//g;, but see File::Spec for some help with volume and path components, if need be. Mind the encoding.

use strict; use warnings; omitted for brevity.

In reply to Re: Why are there no errors when opening a filename that contains colons on Win10 by rjt
in thread Why are there no errors when opening a filename that contains colons on Win10 by Lotus1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.