You can get rid of eval (potential security risk) and even define some new tests like so:

use strict; use warnings; my %tests = ( # key, description, anonymous test function -s => { dsc => "size > 0", ftest => sub { -s $_[0] } }, -T => { dsc => "type Text", ftest => sub { -T $_[0] } }, -B => { dsc => "is Binary", ftest => sub { -B $_[0] } }, -r => { dsc => "is readable", ftest => sub { -r $_[0] } }, -z => { dsc => "is Zero bytes", ftest => sub { -z $_[0] } }, lt100 => { dsc => "small file", ftest => sub { -s $_[0] < 100 } + }, ); my $myfile = $0; print "Tests for '$myfile':\n"; foreach my $t (keys %tests) { my $result = $tests{$t}{ftest}->( $myfile ) || 0; print $result ? 'Passed' : 'Failed'; print " [$t]\t test ($tests{$t}{dsc})\t with result ($result)\n"; } __END__ Tests for 'ftest.pl': Passed [-r] test (is readable) with result (1) Passed [-T] test (type Text) with result (1) Failed [lt100] test (small file) with result (0) Failed [-z] test (is Zero bytes) with result (0) Failed [-B] test (is Binary) with result (0) Passed [-s] test (size > 0) with result (780)
sub { -s $_[0] } is a little shorter than  sub { my $filename = shift; return -s $filename; } which is more comprehensible.

In reply to Re: File testing via list of test operators by Perlbotics
in thread File testing via list of test operators by robh

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.