in reply to Permission & size are not visible

See the second paragraph of readdir:

If you're planning to filetest the return values out of a readdir, you'd better prepend the directory in question. Otherwise, because we didn't chdir there, it would have been testing the wrong file.

Replies are listed 'Best First'.
Re^2: Permission & size are not visible
by gaurav (Sexton) on Jun 19, 2013 at 09:03 UTC

    Hi Corion,Sorry! but I haven't got this answer specially this line "you'd better prepend the directory in question". As its already visible in code :

     opendir DH,"/home/gaurav/Documents" or die "couldn't open the directory :$!";

    that directory is there,if its make you annoy than sorry because m newbie

      Just look at the code after the second paragraph of readdir, where the intended location of "prepend" is shown. The location is not in the readdir() call, but in the later usage, because readdir() only returns the bare name, not the full path to the directory entry.

      @dots = grep { /^\./ && -f "$some_dir/$_" } readdir($dh);

        Thanks a lot

      Either prepend the directory name when doing the file tests inside the loop:

      my $dir = "/home/gaurav/Documents"; opendir my $dh, $dir or die "Couldn't open $dir: $!"; while (readdir $dh) { next if $_ eq "." or $_ eq ".." ; print $_, " " x (30 - length($_)); my $file = "$dir/$_"; # ...do file tests against $file here }

      Or use chdir to change the working directory before entering the loop where you do the file tests:

      my $dir = "/home/gaurav/Documents"; opendir my $dh, $dir or die "Couldn't open $dir: $!"; chdir($dir); while (readdir $dh) { next if $_ eq "." or $_ eq ".." ; print $_, " " x (30 - length($_)); # ...do file tests against $_ here }
        Thanks @smls...Thanks a lote