[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Odd behaviour with -d ?



In a script script which allows the user to name two files to receive
sorted data, it seemed wise to minimse the chance of a file being opened in
write mode with the name of a file already in existence, with the usual
results.

To this end I wrote a small routine to look around the current directory
and return the names of the objects and whether they were files, folders or
applications. Here it is:

#!perl
#scan.pl -- saved as droplet
if($ARGV[0] =~ /(.*):/) { $dir = $1 }
opendir(HERE, "$dir");
@alltitles = readdir(HERE);
closedir(HERE);
foreach $title (@alltitles) {
    if (-d $title) {print "$title \(folder\)\n";next;}
    if (-B $title) {print "$title \(binary\)\n";next;}
    print "$title\n";
}

On the desktop I put three folders (XXX, YYY, ZZZ) and three text files
(a.txt, b.txt and c.txt). 'Scan.pl' (also on the desktop) returned the
following as expected:

a.txt
b.txt
c.txt
Scan.pl (binary)
XXX (folder)
YYY (folder)
ZZZ (folder)

However if  'Scan' is located in an arbitrary directory elsewhere (such as
an untitled folder on the desktop) it returns this, which seems odd:

a.txt
b.txt
c.txt
untitled folder
XXX
YYY
ZZZ

In other words MacPerl seems to lose the ability to discern file types
unless the script is in the same directory as the files themselves. The
list of names however is correct. Is this how Perl is supposed to behave, I
wonder?

Alan