Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I've not been able to solve the problem of space bands in paths on Mac.
chomp (my $metadata_file = <>); $metadata_file =~ s/\s$//; open FILE, "$metadata_file"; $metadata = join '', <FILE>;
It currently produces a "readline() on closed filehandle FILE" if there is a space in the path. I've done a lot of research and the answer is quoting the path. I think I've done that. I'm running perl5.10.0 on MacOS 10.6.8.
Any help would be greatly appreciated.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Mac Space in Path
by hardburn (Abbot) on May 22, 2013 at 18:51 UTC | |
First off, you should check that the open call returns successfully, e.g.:
Which in this case, I believe, would tell you that the file does not exist. The trouble is that if there's a space in the path, then that really is a part of the file, so you're asking for a completely different file by removing the space. Note that chomp would have already removed trailing spaces, so your listed regex is redundant. I think you can simply pass the filename to open without modifying it to remove spaces. If not, then you need to escape the spaces rather than removing them. For instance, the file name "foo bar.txt" would become "foo\ bar.txt". "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni. | [reply] [d/l] [select] |
|
Re: Mac Space in Path
by vsespb (Chaplain) on May 22, 2013 at 19:36 UTC | |
| [reply] |
|
Re: Mac Space in Path
by fishmonger (Chaplain) on May 22, 2013 at 22:18 UTC | |
I think we need a little more info/context. How are you executing the script? Are you passing the file path as a command line arg, or is it in a file that is passed as an arg or piped to the script? Did you try dumping out $metadata_file (via Data::Dumper) prior to the open call to see if it held the value you expected? There's no need to quote $metadata_file in the open call as demonstrated in the following example outputs:
| [reply] [d/l] [select] |
by bettis (Initiate) on May 23, 2013 at 03:27 UTC | |
Thank you so much for your responses. I implemented and tested different permutations of the suggestions listed here. I also added a line to print the path $metadata_file, so I could keep track of what the script was opening if it was able to open the file and not die:
First, I commented out the line that deletes the trailing space from the user input. I noticed that the chomp() was not working the same after OSX 10.4, so I had to implement the line (We're a small Mac typehouse and all of our machines have slightly different implementations of perl. The trailing space works on some machines and not on others, but no trailing space works on all.): $metadata_file =~ s/\s$//;When I comment it out, you'll see that the trailing space is still there and the space between "vendor" and "instructions" has been escaped:
The script opens the file and reads it in perfectly if I simply delete the space in the path:
I also tried not using quotes around $metadata_file in the open command and it successfully commented out the space:
To answer your question, fishmonger, the metadata_file is simply being inserted at the top of every document the script produces, so I open it, join it, format it and insert it when I need to in the script. This particular piece of code is my test to get it to work, then I can rewrite the other parts of the script that read and write files with space bands. Previously, I simply admonished all our comps to not use space bands in their paths. Unfortunately, the client's CMS inserts space bands at every level, so I need to figure this out. It seems so simple, but it's not working. Again, thank you for your help. | [reply] [d/l] [select] |
by bettis (Initiate) on May 23, 2013 at 04:40 UTC | |
Solution: $metadata_file =~ s/\\//;Apparently, Mac OSX Perl doesn't want the space band escaped. It likes a bare space in the path. Again, thank you for your help. | [reply] [d/l] |
by Anonymous Monk on May 25, 2013 at 07:36 UTC | |