Issue with IsDirectory -d

A

Ant

Hi

Has anyone encountered an issue with using the Perl IsDirectory -d?

For example, the following snippet of code should yield 3 invalid
directory responses.

my $folderExists = -d "C:Foo";
my $folderExists2 = -d "C:\\Foo";
my $folderExists3 = -d "";

None of the above folders/directories exist (using XP Pro) yet the
result of $folderExists is 1.
The others yield undefined values which is/should be correct.

Has anyone encountered this or is C:Foo being interpreted as something
else (my only assumption - like C: on its own).

Ant
 
A

A. Sinan Unur

For example, the following snippet of code should yield 3 invalid
directory responses.

my $folderExists = -d "C:Foo";
my $folderExists2 = -d "C:\\Foo";
my $folderExists3 = -d "";

None of the above folders/directories exist (using XP Pro) yet the
result of $folderExists is 1.

Please post a short but complete script along with its output when you
post code. It is hard to tell what you are printing and how.
The others yield undefined values which is/should be correct.

All of the various methods below give the expected results for me
(c:/opt exists and is a directory on my system).

#!/usr/bin/perl

use warnings;
use strict;

use Data::Dumper;

my @dirs = ('C:Foo', 'C:/Foo', q{}, 'C:/opt');

{
my @flags = map { -d $_ ? 1 : 0 } @dirs;
print "$_\n" for @flags;
}

{
my @flags = map { -d $_ } @dirs;
print Dumper \@flags;
}

{
my @flags;

for my $i ( 0 .. $#dirs ) {
$flags[$i] = -d $dirs[$i];
}

print Dumper \@flags;
}

{
my $e1 = -d 'C:Foo';
my $e2 = -d 'C:/Foo';
my $e3 = -d q{};

print "$_\n" for $e1, $e2, $e3;
}
__END__
Has anyone encountered this
No.

or is C:Foo being interpreted as something
else (my only assumption - like C: on its own).

C:Foo is *NOT* the same as C:\Foo. I think C:Foo refers to something
called Foo in the current directory on drive C which, in your case,
seems to have a subdirectory called Foo.

Or, there is something else going on, but it is hard to know because you
did not show the exact code you ran.

For example:

D:\Home\asu1\UseNet\clpmisc> cat ddd.pl
#!/usr/bin/perl

use warnings;
use strict;

my @dirs = ('C:Foo', 'C:/Foo', q{}, 'C:/opt');

{
my @flags = map { -d $_ ? 1 : 0 } @dirs;
print "$_\n" for @flags;
}

__END__

D:\Home\asu1\UseNet\clpmisc> ddd
1
0
0
1

D:\Home\asu1\UseNet\clpmisc> c:

C:\opt\psdk> dir | grep Foo
05/10/2006 09:45 AM <DIR> Foo


--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
A

Ant

ok, apologies for not being clearer.

The code I ran is complete as originally posted minus some print
statements for each $folderExists*.
I get a 1 for C:Foo and 2 undefinds.
I did say that none of the folder C:Foo C:\\Foo or "" exist on my drive
so all should yield undefined should they not.
The code snippet you have posted is a alot more detailed than mine but
all, I guess, I am after is why does -d return different results when
all the folders/directories do not exist?.

Ant
 
A

A. Sinan Unur

ok, apologies for not being clearer.

Please quote some context when you reply.
The code I ran is complete as originally posted minus some print
statements for each $folderExists*.

Post the exact code you used and the output. Nothing minus, nothing
edited.

Please do read and follow the posting guidelines. That will maximize
your chances of solving your problem.
I get a 1 for C:Foo and 2 undefinds.
I did say that none of the folder C:Foo C:\\Foo or "" exist on my
drive so all should yield undefined should they not.

Did you read my response completely? As I pointed out C:Foo is not
determinate. Which file location it refers to depends on what the
current directory on drive C: is when your script runs.
The code snippet you have posted is a alot more detailed than mine but
all, I guess, I am after is why does -d return different results when
all the folders/directories do not exist?.

There is nothing wrong with -d: It does not indicate a directory exists
when it does not. C:Foo does exist on your system. It is just not the
location you think it is.

Post the *EXACT* code you ran.

#!/usr/bin/perl

use warnings;
use strict;

use File::Spec::Functions qw( rel2abs canonpath );

my $dir = 'C:Foo';

printf "%s => %d\n", canonpath(rel2abs $dir), -d $dir;

__END__

D:\Home\asu1\UseNet\clpmisc> ddd
C:\opt\psdk\Foo => 1

D:\Home\asu1\UseNet\clpmisc> c:

C:\opt\psdk> cd \

C:\> d:

D:\Home\asu1\UseNet\clpmisc> ddd
Use of uninitialized value in printf at D:\Home\asu1\UseNet\clpmisc
\ddd.pl line 10.
C:\Foo => 0

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
A

Ant

Post the exact code you used and the output. Nothing minus, nothing
There is no need now, your response at the end hit the nail on the
head. Thank you Sinan.

Agreed and this is what I have missed. The old cwd issue, damn it!. I
missed this totally and its such as a basic thing to miss.

Thanks for your help Sinan.

Ant
 
A

A. Sinan Unur

There is no need now, your response at the end hit the nail on the
head. Thank you Sinan.

You are welcome. Glad it helped.

And thank you for adopting a better follow-up style. However, I would ask
you to also include attributions when you reply.

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top