Home > Archive > PERL Beginners > May 2007 > Inconsistent results from file test (-X) operators
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Inconsistent results from file test (-X) operators
|
|
| R Chandrasekhar 2007-05-16, 9:58 pm |
| Dear Folks,
I have encountered inconsistent behaviour with the file test (-X) operators. I
am using perl, v5.8.8 built for i486-linux-gnu-thread-multi on a Kubuntu Feisty
system.
I show below my minimal test file:
----------- Minimal Test File -----------
#!/usr/bin/perl -w
use diagnostics;
# Minimal example for problem with file test, or -X, operators
$testdir = "/usr";
opendir(DH, $testdir) or die "Could not open $testdir: $!\n";
while (defined($file = readdir(DH)))
{
if (-d $file)
{
print "$file is a directory\n";
}
elsif (-f $file)
{
print "$file is a regular file\n";
}
else
{
print "$file is neither a directory nor a regular file\n";
}
}
closedir(DH);
-----------------------------------------
The results I get are:
-----------------------------------------
include is a directory
src is a directory
lib is a directory
sbin is neither a directory nor a regular file
bin is a directory
local is neither a directory nor a regular file
lib32 is neither a directory nor a regular file
games is neither a directory nor a regular file
share is neither a directory nor a regular file
.. is a directory
... is a directory
lib64 is neither a directory nor a regular file
X11R6 is neither a directory nor a regular file
-----------------------------------------
and
ls -al /usr
gives:
-----------------------------------------
drwxr-xr-x 13 root root 4096 2007-05-15 16:18 .
drwxr-xr-x 23 root root 4096 2007-05-15 00:55 ..
drwxr-xr-x 2 root root 69632 2007-05-17 00:12 bin
drwxr-xr-x 2 root root 4096 2007-05-15 08:45 games
drwxr-xr-x 35 root root 4096 2007-05-15 16:10 include
drwxr-xr-x 149 root root 69632 2007-05-16 14:33 lib
drwxr-xr-x 3 root root 4096 2007-05-15 01:33 lib32
drwxr-xr-x 3 root root 4096 2007-05-15 16:18 lib64
drwxr-xr-x 10 root root 4096 2007-04-17 13:19 local
drwxr-xr-x 2 root root 12288 2007-05-17 00:12 sbin
drwxr-xr-x 244 root root 12288 2007-05-16 13:55 share
drwxrwsr-x 4 root src 4096 2007-04-17 13:23 src
drwxr-xr-x 3 root root 4096 2007-04-17 13:21 X11R6
-----------------------------------------
Can anyone please tell me why I get this inconsistent behaviour and how to
overcome it?
I have read of the possibility of a race condition with these operators but I am
unsure whether it applies here.
Thank you.
Chandra
17 May 07
| |
| John W. Krahn 2007-05-16, 9:58 pm |
| R (Chandra) Chandrasekhar wrote:
> Dear Folks,
Hello,
> I have encountered inconsistent behaviour with the file test (-X)
> operators. I am using perl, v5.8.8 built for i486-linux-gnu-thread-multi
> on a Kubuntu Feisty system.
>
> I show below my minimal test file:
>
> ----------- Minimal Test File -----------
> #!/usr/bin/perl -w
use strict;
> use diagnostics;
>
> # Minimal example for problem with file test, or -X, operators
>
> $testdir = "/usr";
>
> opendir(DH, $testdir) or die "Could not open $testdir: $!\n";
>
> while (defined($file = readdir(DH)))
> {
> if (-d $file)
perldoc -f readdir
readdir DIRHANDLE
Returns the next directory entry for a directory opened by
"opendir". If used in list context, returns all the rest of the
entries in the directory. If there are no more entries, returns
an undefined value in scalar context or a null list in list
context.
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.
opendir(DIR, $some_dir) || die "can’t opendir $some_dir: $!";
@dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR);
closedir DIR;
So:
if (-d "$testdir/$file")
> {
> print "$file is a directory\n";
> }
> elsif (-f $file)
And:
elsif (-f "$testdir/$file")
Or more simply:
elsif (-f _)
> {
> print "$file is a regular file\n";
> }
> else
> {
> print "$file is neither a directory nor a regular file\n";
> }
> }
>
> closedir(DH);
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
| |
| Paul Lalli 2007-05-17, 7:58 am |
| On May 16, 9:01 pm, chan...@ee.uwa.edu.au (R Chandrasekhar) wrote:
> I have encountered inconsistent behaviour with the file test (-X) operators. I
> am using perl, v5.8.8 built for i486-linux-gnu-thread-multi on a Kubuntu Feisty
> system.
>
> I show below my minimal test file:
>
> ----------- Minimal Test File -----------
> #!/usr/bin/perl -w
> use diagnostics;
>
> # Minimal example for problem with file test, or -X, operators
>
> $testdir = "/usr";
>
> opendir(DH, $testdir) or die "Could not open $testdir: $!\n";
>
> while (defined($file = readdir(DH)))
> {
> if (-d $file)
Read the warning in
perldoc -f readdir
Paul Lalli
|
|
|
|
|