Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
more useful options
 
PerlMonks  

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

Comment on Permission & size are not visible
Select or Download Code
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/$_"; # ...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 }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1039734]
Approved by hdb
Front-paged by perlfan
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2014-04-17 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (449 votes), past polls