To amplify a bit on what hossman said, it's better to use the so-called "-X" operators on file names, before you open the files, because you usually want to know if they exist, are writable, etc, before trying to open them. Of course, if the path/filename doesn't exist, there's no point trying to get more information about it.

And it's also worthwhile to know that every time you use any one of the -X operators on a given file, it fetches all the properties for the file (because that's how the underlying system library function works), and it keeps them in a cache, so if you use another -X function on the same file as the previous -X function, you can use "_" (underscore) instead of the file name or file handle, to use the cached info instead of reading it from disk again.

Plus, I like having things structured for loops, and I hate having to repeat things in my perl code when I shouldn't need to... even if it means using "eval" so that I can loop over the tests:

#!/usr/bin/perl use strict; use warnings; my %test = ( '-f' => 'a data file', '-d' => 'a directory', '-r' => 'readable', '-w' => 'writable', '-x' => 'executable', ); die "Usage: $0 [path/]file.name ...\n" unless @ARGV; for my $file ( @ARGV ) { my $report = "$file does not exist\n"; if ( -e $file ) { $report = "$file exists and is:\n"; for my $t ( sort keys %test ) { $report .= " ... $test{$t}\n" if ( eval "$t _" ); } } print "$report\n"; }

In reply to Re: A Tribute To The Monks Of Wisdom by graff
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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.