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

[MacPerl] NEWBIE problem with -d and -f



I am learning Perl/MacPerl fundamentals using refs MPPE and the Camel book,
and lurking on this list.  My apologies if my terminology is imprecise.

I am trying to write a script to automate processing of files/directories,
so I started from the basics of printing the file names and excluding or
including directories or file names.  I have read the fine manual :) and
the books at my disposal, and thought that using -d or -f should choose the
files to process.  I am using a subroutine to do the processing and have
added print statements to show the variable values before calling and
inside the body of the subroutine.  My development is with MacPerl on my
own machine, but I work in a variety of computing environments not owned by
me and would like to make the code as portable as possible without the
addition of modules that may not be present on those other machines. I am
using MacPerl 5.2.0r4, MacOS 8.6, PB G3/333, saving the script as a
droplet.

The test folder (thisFolder) has four documents and two folders:
one.doc
two.doc
three.doc
finalrpt.doc
empty folder	<--empty folder
Some Folder	<-- non-empty folder.

My impression was that the "if(-f " construction would select only files,
not aliases or directories, but instead it selects nothing.  The "unless
-d" construction should exclude directories, but does not.  I hope someone
can tell me how to get the desired behavior, thanks in advance....


The code:

#!usr/local/bin/perl -w
print "starting file list\n";
# now I check if I was passed a commandline argument or something dropped
on a MacPerl droplet
if( @ARGV){
	print "argv is @ARGV\n";	#print what I was handed
	list_filenames(@ARGV);	#call the processing subroutine
	print "done\n";
} else {				#else print the proper usage
	print "Usage: perl FileLister.pl <directory name>\nMac Usage: drop
a directory on the compiled droplet\n"
}
exit;

sub list_filenames{
	my($dir)=@_;
	print "list_filenames directory is $dir\n";	#show what
parameter was passed
	$dir||="./"; #sets to current directory if it wasn't passed a
parameter
	opendir DIR, $dir || warn "couldn't open directory";
	while(defined($file=readdir DIR)){
		#print the name if it is not a directory(should exclude
only directories)
		print "file is $dir:$file\n" unless -d $file;
		if(-f ($dir.$file)){
			#print it if it is a file (should exclude
directories, aliases, etc.)
			print "$file\n";
		}
	}
}
_END_

the output:
starting file list
argv is Zoot:Desktop Folder:thisFolder
list_filenames directory is Zoot:Desktop Folder:thisFolder
file is Zoot:Desktop Folder:thisFolder:empty folder
file is Zoot:Desktop Folder:thisFolder:finalrpt.doc
file is Zoot:Desktop Folder:thisFolder:one.doc
file is Zoot:Desktop Folder:thisFolder:Some Folder
file is Zoot:Desktop Folder:thisFolder:three.doc
file is Zoot:Desktop Folder:thisFolder:two.doc
done

Jane

==============================================================
Logic is the beginning of wisdom, Valeris, not the end.  You must have faith
that the universe will unfold as it should....
		=== Spock, in "Star Trek VI: The Undiscovered Country"
==============================================================

# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org