Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Why are there no errors when opening a filename that contains colons on Win10

by rjt (Curate)
on Oct 09, 2019 at 22:18 UTC ( [id://11107270]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Why are there no errors when opening a filename that contains colons on Win10
in thread Why are there no errors when opening a filename that contains colons on Win10

Exactly, that goes back to what I'm saying: every operating system has its own rules for valid pathnames, and its own unique semantics for what happens when you step outside of those rules. Perl, for the most part, respects those semantics, leaving it to the programmer to decide how best to handle them. If you ask to open a file, Perl passes that request along to the OS, and the return value you get is a function of what the OS itself returns. The errors for your other cases are coming from Windows, not Perl.

I completely agree this Win32 behavior is complex and weird in spots, but it's Win32, not Perl that is giving you this behavior.

I don't follow what you mean about random characters in the output files. The print statement wrote to the files that were created successfully except for test character 8, the colon. In that case the file is empty. It seems to be a valid filehandle but the print didn't write to the file and the print seemed to succeed since it returned a true value.

See the Alternate Data Streams discussion in the MSDN article I linked. And add that to the list of surprises in support of validating your filenames! If you read back the file with the same script, you will actually get the contents back, even though foo appears to exist but be empty (and type foo outputs nothing):

use autodie; my $filename = 'foo:bar.txt'; if ($ARGV[0]) { say "Skipping write."; } else { say "Writing $filename. Run $0 -skip to skip writing."; open my $fh, '>', $filename; say $fh 'Test text'; close $fh; } open my $read, '<', $filename; print "$filename: ' . <$read>; close $read; __END__ Test text

This "works" because we're opening the Alternate Data Stream "bar.txt" of the file "foo". If you delete "foo", then "foo:bar.txt" will no longer be readable either.

And this nicely highlights the general issue: Perl doesn't know or care what you are opening, only whether it succeeds. Perl trusts you to know what you are asking the OS to do. Similarly, if the Windows system call confirms 12 bytes were written, Perl will take Windows' word for it. It's up to you to verify the bytes written if desired, which isn't always possible (e.g., when writing to sockets). Knowing the semantics of your target platform(s) is incumbent on you whenever you are dealing with system-level code.

Lastly, I do understand that this is experimental for you, and you have uncovered some interesting Win32 behavior. I hope these insights are helpful in answering your question.

use strict; use warnings; omitted for brevity.

Replies are listed 'Best First'.
Re^4: Why are there no errors when opening a filename that contains colons on Win10
by Lotus1 (Vicar) on Oct 11, 2019 at 20:32 UTC

    rjt, Thanks for the follow up. I misunderstood a few things you said in your posts. The random characters you were referring to were characters in the filename. I only put a random character in my filename to test my logic in case there was a problem with opening a file. Also, I noticed on rereading your post you were talking about the colon being the volume separator not a path separator.

    Thanks for the link to File::Spec. It looks very useful for the kind of backend server work I do.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11107270]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-19 14:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found