Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How do you open a filehandle to a DOS file which has spaces in the filename?

by Anonymous Monk
on Apr 28, 2000 at 21:59 UTC ( [id://9615]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (input and output)

using perl, of course. no one here at work listens to me when i try to get them to take off the spaces in filenames.

Originally posted as a Categorized Question.

  • Comment on How do you open a filehandle to a DOS file which has spaces in the filename?

Replies are listed 'Best First'.
Re: How do you open a filehandle to a DOS file which has spaces in the filename?
by grinder (Bishop) on May 28, 2002 at 13:28 UTC
    This is not a problem unique to DOS, all operating systems are prone to this problem. What is worse, if the file has leading spaces, Perl will silently strip those spaces out, causing the call to fail.

    If you don't believe this, try running the following commands (under Unix).

    % echo foo >' leading space'; % perl -le 'open I, " leading space" or print "bletch: $!"'

    This outputs bletch: No such file or directory.

    This is due to the way Perl looks for < and > meta-characters to determine what mode to use (input or output). The most reasonable portable and documented method to work around this bug is to use sysopen in the place of open. In any event, you will need to use the Fcntl module to get this working correctly (part of the core distribution).

    Here are the most common cases to get you started:

    modeopensysopen
    input open IN, 'a file.txt' sysopen IN, 'a file.txt', O_RDONLY
    output open OUT, '>a file.txt' sysopen OUT, 'a file.txt', O_WRONLY
    output append open OUT, '>>a file.txt' sysopen OUT, 'a file.txt', O_WRONLY|O_APPEND

    Just slap a use Fcntl at the top of your program, and drop in the sysopen replacements for each open statement, remembering of course to test the truth (success) of the statement and printing the contents of $! when things go awry.

    See also Two-arg open() considered dangerous.

Re: How do you open a filehandle to a DOS file which has spaces in the filename?
by turnstep (Parson) on Apr 28, 2000 at 22:10 UTC
    Just quote it and everything will be fine:
    open (SPACEY, "this file has spaces.txt") || die;
      Thanks a lot, i also dealt with issue for several hours and could not find a solution in any of my manuals. Wish I knew how to use the search feature a little better when i wrote this code.
        my $filename = "this file has spaces.txt" open (SPACEY, "$filename") || die;
        Hardcoded filenames are unwise. Shouldn't the above work also? Didn't seem to.
Re: How do you open a filehandle to a DOS file which has spaces in the filename?
by Anonymous Monk on May 07, 2004 at 07:53 UTC
    Just looking for a sample which does not have the filename hardcoded. eg.
    my $filename = "this file has spaces.txt"; sysopen IN, $filename, O_RDONLY;
    Didn't do much for me.

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-25 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found