So, I hereby dedicate this to every Monk on here, that has ever taken the time to answer a newbie question, in an effort to further a fellow Monk's knowledge.
In the spirit of furthering a fellow Monk's knowledge with constructive criticism...
- It's a good idea to get into the habbit of writing "close" anytime you write "open" ... perl will close any open filehandles for you when your script ends, but even in really short scripts you could conceivable run out of file handles if you use a lot of them (or if your code gets refactored into a longer running program).
- if you declare a counter (ie: $i) just for hte purposes of looping over an array, consider foreach instead. This...
foreach my $filename (@ARGV) {
open(MYFILE, $filename)
or die("Error: cannot open file '$filename'\n");
print "$filename is readable!\n" if -r MYFILE;
print "$filename is writable!\n" if -w MYFILE;
print "$filename is executable!\n" if -x MYFILE;
print "$filename exists!\n" if -e MYFILE;
}
...is a little bit easier to read (to me) then what you've got right now, and a little less error prone to off by one errors and/or typos (hint: you're using $ARGV[0] in a place where you probably ment to use $ARGV[$i]
- the file test operators (-r, -x, etc...) can take a filename as an expression, so you don't *have* to open the files first -- in fact if the file doesn't exist, or isn't readable, you won't be able to open it, so some of those test are redundant. :)