Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Permission & size are not visible

by gaurav (Acolyte)
on Jun 19, 2013 at 06:54 UTC ( #1039734=perlquestion: print w/replies, xml ) Need Help??
gaurav has asked for the wisdom of the Perl Monks concerning the following question:

Hi,I am very new to PERL.I have been confusing so far regarding directories.I mean,suppose if I run the below code

#!/usr/bin/perl use warnings; use strict; opendir DH, "." or die "couldn't ope $!"; while ($_ = readdir(DH)) { next if $_ eq "." or $_ eq ".." ; print $_, " " x (30 - length($_)); print "d" if -d $_; print "r" if -r _; print "w" if -w _; print "x" if -x _; print "o" if -o _; print "\t"; print -s _ if -r _ and -f _; print "\n"; }

I am getting proper output as file-names ,permission and their sizes. But whenever I had change "opendir" as

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

I have been getting only file-names,neither permissions nor file-size. Any-one can help me regarding this.Thanks in Advance

Replies are listed 'Best First'.
Re: Permission & size are not visible
by Corion (Pope) on Jun 19, 2013 at 06:56 UTC

    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.

      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);

        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/$_"; # 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($_)); # file tests against $_ here }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1039734]
Approved by hdb
Front-paged by perlfan
[james28909]: i see the predicament here.
[james28909]: maybe rename it to 'query'
[james28909]: but then you have the others as well
[Lady_Aleena]: Renaming things like get_THAC0 to just THAC0 was easy. These are harder.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2017-05-24 04:44 GMT
Find Nodes?
    Voting Booth?