http://www.perlmonks.org?node_id=954794

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

On win32/winxp, -r and chdir succeed but opendir fails , permission denied

commands

echo %cd% perl -le " print -r 'Administrator' " perl -le " chdir 'Administrator' or die $! " perl -le " chdir 'Administrator' or die $!; opendir DIR, '.' or die $! + " perl -le " chdir 'Administrator' or die $!; opendir DIR, '.' or die $! +,$/,$^E " perl -e " die $] "

output

$ echo %cd% C:\Documents and Settings $ perl -le " print -r 'Administrator' " 1 $ perl -le " chdir 'Administrator' or die $! " $ perl -le " chdir 'Administrator' or die $!; opendir DIR, '.' or die +$! " Invalid argument at -e line 1. $ perl -le " chdir 'Administrator' or die $!; opendir DIR, '.' or die +$!,$/,$^E " Invalid argument Access is denied at -e line 1. $ perl -e " die $] " 5.014001 at -e line 1.

Is it bug?

Replies are listed 'Best First'.
Re: win32/winxp, -r and chdir succeed but opendir fails, permission denied
by LonelyPilgrim (Beadle) on Feb 18, 2012 at 20:13 UTC

    Seems like it might be more effective to run an actual script to step through the process and diagnose where this is breaking down:

    use strict; use warnings 'all'; use Cwd; my $docs = 'C:\Documents and Settings'; chdir($docs) || die "Couldn't change directory to $docs: $!"; print "Current directory is ", getcwd, "\n"; if (-r 'Administrator') { print "Administrator is readable.\n"; } else { die "Couldn't read 'Administrator': $!"; } if (chdir('Administrator')) { print "Changed directory to Administrator.\n"; } else { die "Couldn't change directory to 'Administrator': $!"; } print "Current directory is ", getcwd, "\n"; if (opendir(ADMINDIR, "$docs\\Administrator")) { print "Opened directory Administrator.\n"; } else { die "Couldn't open directory 'Administrator': $!"; }

    By the way, be sure you're not trying to opendir on your directory after you've chdir'ed into it. I caught that mistake in whipping up the above. To opendir on your current working directory, you either have to opendir(DIR, '.') or provide the full path.

      To opendir on your current working directory, you either have to opendir(DIR, '.') or provide the full path.
      Quoting the OP:
      opendir DIR, '.' or die $!
      which he does right after a successful chdir. Which point are you trying to make here?

        I noted my own mistake. And then noticed that the OP had not made it. I was not implying any incompetence on his part, but acknowledging my own. I didn't remove the comment because I hoped that it might be helpful to someone else.