Perl on Mac OSX does not like ~

This caused me to lose about thirty minutes of my precious “not working, not sleeping” time (currently at about two hours a day.)

I was trying to run some file test operations on some files in OSX using Perl (one of the newer ones, 5.10 maybe) that have spaces in their names. In general, I absolute hate how processing files with spaces is so difficult. One thing I like to do is change the IFS (internal file separator) in bash while doing things with files. This blog post shows a really nice example of doing that.

Anyway, I was doing something basically like:

if (-e $file && -f $file) { … }

for $file where $file was something like “~/tmpLibrary/covers/Sanderson, Brandon – Mistborn.jpg”. It was failing all the time. I was also getting mystifying “failed trying to stat file with newline in it” error messages when I knew the file name had no newlines. I still don’t know what that was about. Unfortunately, for files that I knew existed and were regular, the above test was failing.

Why?

After much experimentation, lots of googling (there was nothing on this) I figured it out. And remembered that I knew this was a problem at some point, and must have forgotten. Perl barf on “~/tmpLibrary/bar.jpg”. It will work fine with absolute file paths: “/Users/devans/tmpLibrary/bar.jpg”. Oh. That is strange, but I guess I can understand that. There isn’t any real reason that Perl should expand the path with the user home location. I wouldn’t be surprised if it, and would be (was!) surprised when it didn’t, but whatever.

So, remember that kids. Don’t count on home directory expansion. Not that I could find anything about that (or spaces in filenames, which work just fine) for perl file operator tests, but there you go.

And I spend my free time doing stuff like this for fun. I am a sad, sad man.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *