Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Readdir problem 1

Status
Not open for further replies.

tanderso

IS-IT--Management
Aug 9, 2000
981
US
I have the following block of code:
Code:
----------------------------------
my $string = "";

my $dir = "$self->{path}";
$dir .= "$folder" if $folder;

print qq~Opening $dir...<br>\n~ if $self->{debug};

opendir(DIR,&quot;$dir&quot;) 
  || return $self->error(200,&quot;Cannot open directory $dir: $!&quot;);
my @files = readdir DIR 
  || return $self->error(200,&quot;Cannot read directory $dir:$!&quot;);
closedir DIR 
  || return $self->error(200,&quot;Cannot close directory $dir: $!&quot;);

print qq~There are ~. scalar @files .qq~ files in the directory...<br>\n~ if $self->{debug};

while (@files)
{
	my $filename = shift @files;
	$string .= qq~$filename<br>\n~;
}

print $string;
----------------------------------
With
Code:
$self->{path} = &quot;.../album&quot;
(it's a full path, but I edited it here),
Code:
$folder = &quot;&quot;
, and
Code:
$self->{debug} = 1
, I get the following output:

Opening .../album...<br>
There are 1 files in the directory...<br>
.<br>

However, I know for a fact that the specified directory (777 BTW) actually contains &quot;..&quot;, &quot;DCP_O146.JPG&quot;, and &quot;DCP_0147.JPG&quot;. So, the question is, why is this code not outputting the filenames which exist in that directory? Or more specifically, why is &quot;readdir&quot; only returning &quot;.&quot;?

BTW, I get the same problem if I do &quot;while (readdir DIR)...&quot; instead of assigning it to an array.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
The problem is that '||' binds more tightly than the assignment operator, '=', so readdir is being called in scalar (boolean) context and therefore only returns the first file in the directory. Change the '||' to 'or', which binds less tightly, and this will change the logic of the statement to what you expect.
[tt]
my @files = readdir DIR
or return $self->error(200,&quot;Cannot read directory $dir:$!&quot;);
[/tt]
jaa
 
Wow, I have to admit that I didn't really think that would solve the problem... I tried it anyway, and it worked! I always thought that &quot;or&quot; and &quot;||&quot; were synonymous. So what is &quot;or&quot; doing differently from &quot;||&quot; then? Is it just a matter of precedence?
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
to my knowledge, the only difference between the two operators is precedence (same with 'and' and '&&'). that can be a significant difference, though. i find myself fighting with precedence sometimes, especially with list operators.

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top