Reputation: 121
I've been banging my head against this same wall for 48 hours. How do you pass a command line argument to Perl so that when Perl has the opportunity to open the file, it properly handles the Windows-style spaces that occur in a) directories or b) file names:
# open( PRELIM, "\"$ifile\"") or die "Cannot open $ifile";
# open( PRELIM, '\"$ifile\"') or die "Cannot open $ifile";
## Both these lines cannot deal with a space present in the path:
# open PRELIM, $ifile or print "\n* Couldn't open ${ifile}\n\n" && return;
# $ifile = qq($ifile); Doesn't help, still leaves the file as if it was 'two' files
#
# Quotes around $ifile do no good either
open( PRELIM, $ifile) or die "Cannot open $ifile"
Generally, the above line is correct until there is a space present in the variable '$ifile'.
Upvotes: 2
Views: 5793
Reputation: 11
I'm not too familiar with Perl, but I was able to look around and I found a forum post that may help you: http://www.tek-tips.com/viewthread.cfm?qid=376686&page=567
They are discussing the exact issue you are having and the first person in there suggested using single quotes instead of double to use a string literal so $DIRECTORY = 'C:\Documents and Settings';
instead of
$DIRECTORY = "C:\Documents and Settings";
I hope this helps!
Upvotes: 1
Reputation: 11
Ensure you are also not using any reserved characters in your filename either.
I made that mistake of using a question mark as a default field value which only materialised in the filename as an issue !
Upvotes: 1
Reputation: 434965
You want the three argument form of open()
:
open(PRELIM, '<', $ifile) or die "Cannot open $ifile" # For reading
open(PRELIM, '>', $ifile) or die "Cannot open $ifile" # For writing
Always use the three argument version of open()
to avoid issues like this.
Upvotes: 3
Reputation: 80355
To pass a command-line argument as a single argument that also contains spaces, enclose it within quotation marks. For example:
perl script-name.pl "C:\My path with spaces"
This will cause the parameter to be handled as a single word.
(Within your code, if you're hard-coding back-slashes, then you need to double them up since "\" is a special meta-character, hence "\" will be converted to "\" at compile-time.)
Upvotes: 4