C
Colin Howarth
Hi, I'm running perl 5.6.0 on Mac OS X (darwin) and have a program with
the following code:
sub do_dir {
my $dir = shift;
....
opendir(DIR, $dir) or die "Can't open directory '$dir' $!";
while ($file = readdir(DIR)) {
next if $file =~ /^\.\.?$/;
next if -l "$dir/$file";
if (-d "$dir/$file") {
do_dir("$dir/$file");
} elsif (-f "$dir/$file") {
open(FILE, "$dir/$file") or die "Can't open
'$dir/$file': $!";
...
}
i.e. it's recursing through the filesystem, skipping symbolic links
(because there are entries linked back to a parent directory (!)) and
skipping '.' and '..' of course.
This works fine except when it comes across a filename like
/Library//CFMSupport/StuffItEngineShell.cfm/Icon^M
i.e. where the last character is a CR, when the program dies.
Now, I don't know why the odd file has a CR in it's name, but why
doesn't open() work anyway?
At present I've resorted to
# chop $file if $file =~/\r$/;
chop $file if substr($file, -1, 1) eq "\r";
(substr seems to be slightly quicker here)
Thanks,
colin
the following code:
sub do_dir {
my $dir = shift;
....
opendir(DIR, $dir) or die "Can't open directory '$dir' $!";
while ($file = readdir(DIR)) {
next if $file =~ /^\.\.?$/;
next if -l "$dir/$file";
if (-d "$dir/$file") {
do_dir("$dir/$file");
} elsif (-f "$dir/$file") {
open(FILE, "$dir/$file") or die "Can't open
'$dir/$file': $!";
...
}
i.e. it's recursing through the filesystem, skipping symbolic links
(because there are entries linked back to a parent directory (!)) and
skipping '.' and '..' of course.
This works fine except when it comes across a filename like
/Library//CFMSupport/StuffItEngineShell.cfm/Icon^M
i.e. where the last character is a CR, when the program dies.
Now, I don't know why the odd file has a CR in it's name, but why
doesn't open() work anyway?
At present I've resorted to
# chop $file if $file =~/\r$/;
chop $file if substr($file, -1, 1) eq "\r";
(substr seems to be slightly quicker here)
Thanks,
colin