Table of Contents


For this exercise, I suggest creating another directory where you have at least two text files and two or more binary files. Copy a couple of .dll files from your WINDIR directory if you need to, those will do for the binaries, and save a couple of random text files. Size doesn't matter, in this case.

Then run this, giving the directory as the command line argument:

$dir=shift;	# shifts @ARGV, the command line arguments after the script name

chdir $dir or die "Can't chdir to $dir:$!\n" if $dir;

while (<*>) {
	print "Found a file: $_\n" if -T;

The chdir function changes perl's working directory. You should, as ever, test to see if it worked or not. In this case we only try and change directory if $dir is true.

The <*> construct reads all files from a given directory, and prints if it passes the file test -T , which returns true if the file is a non-binary, ie text file. You can be more specific:

$dir =shift;

chdir $dir or die "Can't chdir to $dir:$!\n" if $dir;

while (<*.$type>) {
	print "Found a file: $_\n";

like so. But, there is a better way to read from directories. The method above is rather slow and inflexible.