Re: if (-d$_) failes because of white space
by Anonymous Monk on May 13, 2009 at 11:18 UTC
|
You are misdiagnosing the problem, because whitespace isn't a problem.
#!/usr/bin/perl --
mkdir 'i got white space';
mkdir 'i got white space/one';
mkdir 'i got white space/o n e';
mkdir 'i got white space/o ne';
mkdir 'i got white space/one';
opendir INDIR, 'i got white space';
print "($_)\n" for readdir INDIR;
closedir INDIR;
__END__
(.)
(..)
(o n e)
(o ne)
(one)
| [reply] [Watch: Dir/Any] [d/l] |
Re: if (-d$_) failes because of white space
by cdarke (Prior) on May 13, 2009 at 11:35 UTC
|
Works for me, are you sure you are not passing these names through the command-line (@ARGV) and missing quotes? Or maybe you are using < > globbing? | [reply] [Watch: Dir/Any] |
Re: if (-d$_) failes because of white space
by Utilitarian (Vicar) on May 13, 2009 at 12:07 UTC
|
mkdir test\ this
perl -e 'opendir (DIR,"./");@files = readdir DIR;foreach $file (@files
+){print "$file is a directory\n" if (-d $file);}'
test this is a directory
.. is a directory
. is a directory
Could you provide a wider picture of the issue, perhaps an ls -lR of the directory structure and a larger code snippet?
| [reply] [Watch: Dir/Any] [d/l] |
Re: if (-d$_) failes because of white space
by leocharre (Priest) on May 13, 2009 at 15:09 UTC
|
use Cwd;
foreach(@dir) {
if (-d$_) { print $_," is a directory.\n"; }
else { die("Not a dir '$_', current working directory is: ". cwd())
+; }
}
| [reply] [Watch: Dir/Any] [d/l] |
Re: if (-d$_) failes because of white space
by ikegami (Patriarch) on May 13, 2009 at 16:08 UTC
|
Not enough info. There are two likely possibilities (neither of which have anything to do with -d).
Are you using glob without realizing that spaces are metacharacters for glob? Use File::Glob's bsd_glob instead. It's the same function, but it treats spaces as "ordinary" characters.
Are you using the result of readdir (an unqualified file name) without prepending the path of that file name?
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: if (-d$_) failes because of white space
by radiantmatrix (Parson) on May 13, 2009 at 15:53 UTC
|
You need some diagnostics, friend (and code tags...):
use strict; use warnings;
use Data::Dumper::Simple;
foreach my $d (@dir) {
if (-d $d) {
print $d, "is a directory.\n";
}
else {
warn "Not a directory:".Dumper(\$d);
}
}
Try that and let us see your output. FWIW, you should also take a moment and read How (Not) To Ask A Question to help you get better answers faster.
<–radiant.matrix–>
Ramblings and references
“A positive attitude may not solve all your problems, but it will annoy enough people to make it worth the effort.” — Herm Albright
I haven't found a problem yet that can't be solved by a well-placed trebuchet
| [reply] [Watch: Dir/Any] [d/l] |
Re: if (-d$_) failes because of white space
by Gretter (Novice) on May 13, 2009 at 15:54 UTC
|
In bash i do:
export ril=~/.mozilla/firefox/profiledirectory/Read-It-Later\ Offline/
+f
echo $ril
/home/usrname/.mozilla/firefox/profiledirectory/Read-It-Later Offline/
I call the script like this:
myscript.pl "$ril"
this is the script:
#!/usr/bin/perl -w
$dir = shift or die("Use directory as argument.\n");
print $dir,"\n";
opendir DIR,$dir or die "Can't open directory.\n";
@dir = readdir DIR;
closedir DIR;
foreach(@dir) {
if (-d$_) {print $_," is a directory.\n";}
elsif (-f$_) {print $_," is a file.\n";}
else {print $_," is not a file or a directory!\n";}
}
this is the out put of the script:
/home/usrname/.mozilla/firefox/profiledirectory/Read-It-Later Offline/
. is a directory.
.. is a directory.
RIL 533_files is not a file or a directory!
list_of_reports.html is a file.
RIL 533.html is not a file or a directory!
RIL 1178.html is not a file or a directory!
RIL 1178_files is not a file or a directory!
| [reply] [Watch: Dir/Any] [d/l] |
|
foreach(@dir) {
my $path = "$dir/$_"; # prepend dir
if (-d $path) {print $path," is a directory.\n";}
elsif (-f $path) {print $path," is a file.\n";}
else {print $path," is not a file or a directory!\n";}
}
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: if (-d$_) failes because of white space
by nimdokk (Vicar) on May 13, 2009 at 18:44 UTC
|
Try wrapping quotes around $_.
my $dir = "\'$_\'";
Some OS platforms don't like those spaces (even tho it allows you to put them into the directory names *cough*windows*cough* :-) | [reply] [Watch: Dir/Any] [d/l] |