PinkElephantsOnParade
PinkElephantsOnParade

Reputation: 6592

Why can't Perl see this directory?

I have this directory structure:

$ ls -F
analyze/
data.pl
input.pl
logminer.txt
logSearch.pl
readFormat.pl
Version Notes.txt
datadump.pl
format/
logminer.pl
logs/
properties.txt
unzip.exe

I run:

perl -e 'if (!(-d analyze)){ print "poo\n"}'

and it prints poo.

What is missing here? I have done tests like this earlier and it would correctly identify that the directory exists. Why not this directory?

Upvotes: 1

Views: 77

Answers (2)

ikegami
ikegami

Reputation: 385657

First,

-d analyze

means "check if the file handle anaylyze is a directory handle". You want

-d 'analyze'

Now, you say you still get the problem by doing that, so check what error you're getting.

my $rv = -d 'analyze';
die "stat: $!" if !defined($rv);
die "Not a dir" if !$rv;

-d is just a thin wrapper around stat(2), so it's not Perl that "can't see", it's the system.

The most common errors:

  • The current work directory isn't what you think it is. (Many people assume it's always the directory in which the script resides.)
  • The file name has trailing whitespace, especially a newline. (That's not likely to be the case here.)

Upvotes: 2

Marc B
Marc B

Reputation: 360632

perl -e 'if (!(-d "analyze")){ print "poo\n"}'
                  ^--     ^---

missing quotes?

edit: changed to double quotes - forgot this was for command-line perl

Upvotes: 5

Related Questions