Looping Dir entires with space

From: Eric Anderson (eric_at_afaik.us)
Date: 01/28/05


Date: Thu, 27 Jan 2005 20:05:30 -0500

I am developing a Perl CGI application that needs to work on both
Windows and UNIX. In a couple of places in the app, I read a list of
files from a directory. My basic code is something like:

my $config_dir = '/conf/';
if( -d $config_dir ) {
        my $file;
        while(defined($file=<$config_dir*>)) {
                print "$file\n";
        }
}

This seems to work just fine. But if the variable $config_dir has a
string with a space in it (e.g. '/Program Files/Foo Bar') then it
doesn't seem to list the files correctly. The app seems to be reading
each part of the path (split on the space) as it's own file. So my
output will be something like:

/Program
Files/Foo

So my first thought is to change the code to the following:

my $config_dir = '/Program Files/';
if( -d $config_dir ) {
        my $file;
        while(defined($file=<"$config_dir"*>)) {
                print "$file\n";
        }
}

This makes it work fine with there is a space in the path but now it
doesn't work when there is not a space in the path. If $config_dir is
set to '/conf/' I don't get a listing at all.

So my question is how do I iterate through a list of files in a
directory in such a way that will work on both UNIX and Windows and work
when the path has a space and doesn't have a space. Also sometimes I
will need to do stuff like:

while(defined($file=<$config_dir/foo/bar/*>)) {
        print "$file\n";
}

I need this to work when $config_dir has a space and doesn't have a
space. I checked google and the various perl man pages but wasn't able
to find the answer I am looking for. Any help would be greatly appreciated.

Eric



Relevant Pages

  • Re: freebsd as the basis for something better?
    ... New gui tools are needed. ... To bring UNIX to the masses one of the first things we need to do is ... was called "DLL Hell" in windows 3.x, is this the best we can do? ... Download the app from developer foo's website. ...
    (freebsd-questions)
  • Re: MacMini and Unix
    ... Windows, in face i'm struggling. ... I bought the thing to practice on Unix as I lease some Unix servers to run ... an app on.... ... FOR THE LOVE OF FUCKING GOD. ...
    (comp.sys.mac.advocacy)
  • Re: MacMini and Unix
    ... I bought the thing to practice on Unix as I lease some Unix servers to run an app on.... ... Just to add extra IP's, a DNS Zone, and get a mail server working with my mailing app is simply fucking impossible. ... Stuff that I can do in under 5 minutes on Windows 2003. ...
    (comp.sys.mac.advocacy)
  • Re: MacMini and Unix
    ... Windows, in face i'm struggling. ... I bought the thing to practice on Unix as I lease some Unix servers to ... an app on.... ... FOR THE LOVE OF FUCKING GOD. ...
    (comp.sys.mac.advocacy)
  • Re: Looping Dir entires with space
    ... > I am developing a Perl CGI application that needs to work on both ... > Windows and UNIX. ... My basic code is something like: ... > directory in such a way that will work on both UNIX and Windows and work ...
    (comp.lang.perl.misc)