E
Eric Anderson
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
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