Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Re: A Tribute To The Monks Of Wisdom by blazar
in thread A Tribute To The Monks Of Wisdom by koolgirl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-26 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found