Say I create an array
@tests=("d","f","x")
and I want to run the file test for that character on a file, in a loop.
foreach $t (@tests) {
if (-$t $file) {print "yes"}
}
(this of course doesn't work)
How can I do this? Even
if (eval "-$t $file") {print "yes"}
doesn't seem to work.
Perl file test using variable for operator?
I think if you use strict and use warnings (like you should in EVERY program, no matter how small), the answer would become clear. In any case, here is some TESTED code:
use strict;
use warnings;
my @acTest = qw( d f x );
opendir(DIR, '.') or die $!;
ENTRY:
while (my $sEntry = readdir DIR)
{
TEST:
foreach my $cTest (@acTest)
{
my $sTest = qq{ -$cTest "$sEntry"};
my $res = eval $sTest;
if ($@)
{
print STDERR "eval of =$sTest= failed: $@\n";
next TEST;
} # if
print STDERR "result of =$sTest= is =$res=\n";
} # foreach
} # while
closedir DIR or warn $!;
__END__
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment