Issue with IsDirectory -d

Discussion in 'Perl Misc' started by Ant, May 10, 2006.

  1. Ant

    Ant Guest

    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
     
    Ant, May 10, 2006
    #1
    1. Advertising

  2. "Ant" <> wrote in news:1147267243.388953.307580
    @i39g2000cwa.googlegroups.com:

    > 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 <>
    (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. Sinan Unur, May 10, 2006
    #2
    1. Advertising

  3. Ant

    Ant Guest

    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
     
    Ant, May 10, 2006
    #3
  4. "Ant" <> wrote in news:1147269547.725215.95260
    @u72g2000cwu.googlegroups.com:

    > 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 <>
    (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. Sinan Unur, May 10, 2006
    #4
  5. Ant

    Ant Guest

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


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

    >>C:Foo does exist on your system. It is just not the
    >>location you think it is.


    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
     
    Ant, May 10, 2006
    #5
  6. "Ant" <> wrote in news:1147272391.564395.22570
    @j73g2000cwa.googlegroups.com:

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

    >
    > 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 <>
    (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. Sinan Unur, May 10, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JohnFol
    Replies:
    0
    Views:
    1,352
    JohnFol
    Aug 6, 2003
  2. Satinderpal Singh

    AppDomain Issue...

    Satinderpal Singh, Oct 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    712
    Dan Ignatov
    Oct 27, 2003
  3. Catherine Jones

    rich text box issue

    Catherine Jones, Dec 11, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    742
    Robert Jacobson
    Dec 11, 2003
  4. AJ

    Datatype issue

    AJ, Jan 12, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    503
    Mattias Sjögren
    Jan 12, 2004
  5. Srini
    Replies:
    11
    Views:
    994
    Arne Vajhøj
    Jun 1, 2008
Loading...

Share This Page