http://qs321.pair.com?node_id=725105


in reply to A Tribute To The Monks Of Wisdom

I personally believe that I will also comment on your code. I've given a glance at the other responses thus far, but I'm replying as if I only saw your post: you'll surely notice some repetitions with comments by others, this should help to stress some issues it has, and in particular to show you why it was a bad idea to post it in this section to start with.

#!usr/bin/perl use strict;

"Wrong" shebang line (but it won't matter if you're under Windows - not a good reason not to put it right there too: it will help portability!) And missing use warnings; line too.

# This is a program which reads in a list of file names from the comma +nd # line and prints which files are readable, writable and/or executable +, # and whether each file exists.

Not terribly compelling, but for a snippet you may have considered using a tiny piece of POD rather than comments.

while ($i < scalar(@ARGV)) { open(MYFILE, $ARGV[$i]) or die("Error: cannot open file '$ARGV[$i] +'\n");

Three issues:

print "$ARGV[$i] is readable!\n" if -r MYFILE; print "$ARGV[$i] is writable!\n" if -w MYFILE; print "$ARGV[$i] is executable!\n" if -x MYFILE; print "$ARGV[$i] exists!\n" if -e MYFILE; $i++;

In terms of logic and UI: are you sure about that order? Incidentally, if the file didn't exist, could have you opened for reading?

} # end while

If you really needed that, then probably you wanted a language which supports such specifications at the level of the syntax. Or else you'd have a too big a while loop, in which case I'd recommend you to trim it down. All in all I would rewrite your program like

#!/usr/bin/perl use strict; use warnings; use 5.010; for my $file (@ARGV) { -e $file or next; say "`$file' exists!"; say "`$file' is ", ( $_->[1]() ? "" : "not " ), $_->[0] for [readable => sub () { -r _ }], [writable => sub () { -w _ }], [executable => sub () { -x _ }]; } __END__
--
If you can't understand the incipit, then please check the IPB Campaign.