Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: A Tribute To The Monks Of Wisdom

by blazar (Canon)
on Nov 21, 2008 at 11:56 UTC ( #725105=note: print w/replies, xml ) Need Help??

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:

  • in [Pp]erl, TMTOWTDI, but there are indeed prefer{red,able} WTDI for some specific and common tasks: please let me tell you that the way you adopt to iterate over @ARGV is the most uselessly byzantine I could think of. Incidentally, Perl DWIM most of the times to help you write clean and terse code: that scalar is redundant there;
  • if you're using the two-args form of open you may well rely on *ARGV's implicit open() instead. But then you use file tests in which case I would avoid opening at all unless you have a good reason to so. However, if you do then all of the remaining "standard" recommendations about open() apply too: in particular those about using the three-args form, and lexical handles. I'm not repeting here why it's good to follow them because it is done ad nauseam on a daily basis;
  • it would also be good to say people why something went wrong, and thus include $! in the error message.
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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://725105]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2021-10-25 09:31 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (89 votes). Check out past polls.