problem using eval with array of file test operators 
Author Message
 problem using eval with array of file test operators

Hi.  I am trying to write a method which prints out the status of a
file using the file test operators.  The code is shown below.  I am
having a problem trying to figure out how to set up the command text
string and using eval.  I keep getting the error 'Scalar found where
operator expected at (eval 50) line 1, at end of line (#2)'.  The
operation never failes either, even though the tests are mutually
exclusive.

Please someone explain this to me.

Curtis
================================================================

sub file_tests
{
  my $self = shift;


                 ['-r', "File is %s readable by effective uid\n" ],
                 ['-w', "File is %s writable by effective uid\n" ],
                 ['-x', "File is %s executable by effective uid\n" ],
                 ['-o', "File is %s owned by effective uid\n" ],
                 ['-R', "File is %s readable by real uid\n" ],
                 ['-W', "File is %s writable by real uid\n" ],
                 ['-X', "File is %s executable by real uid\n" ],
                 ['-O', "File is %s owned by real uid\n" ],
                 ['-e', "File does %s exist\n" ],
                 ['-z', "File does %s have zero size\n" ],
                 ['-s', "File does %s have zero non-size\n" ],
                 ['-f', "File is %s a plain file\n" ],
                 ['-d', "File is %s a directory\n" ],
                 ['-l', "File is %s a symbolic link\n" ],
                 ['-S', "File is %s a socket\n" ],
                 ['-p', "File is %s a named pipe\n" ],
                 ['-b', "File is %s a block file\n" ],
                 ['-c', "File is %s a character special file\n" ],
                 ['-u', "File does %s have setuid bit set\n" ],
                 ['-g', "File does %s have setgid bit set\n" ],
                 ['-k', "File does %s have sticky bit set\n" ],
                 ['-t', "Filehandle is %s opened to a tty\n" ],
                 ['-T', "File is %s a text file\n" ],
                 ['-B', "File is %s a binary file\n" ]
              );

  my( $ftest, $result, $qualifier, $ratest, $command );

    $ftest = $$ratest[0];
    $result = $$ratest[1];

    $command =    
    'if ( $ftest $fname ) {
      $qualifier = "";
    } else {
      $qualifier = "not";
    }';

    print "Command is $command\n";  
    eval $command ;
    printf($result, $qualifier);
  }

Quote:
}



Fri, 19 May 2000 03:00:00 GMT  
 problem using eval with array of file test operators


Quote:
> Hi.  I am trying to write a method which prints out the status of a
> file using the file test operators.  The code is shown below.  I am
> having a problem trying to figure out how to set up the command text
> string and using eval.  

Why should you use eval? It's better to avoid eval STRING, in general. But
if you want to use many filetest operators on the same file, you can
save a lot of time if you just stat it once, then use the underscore
pseudo-filehandle.


        unless (stat $filename) {       # Load up the info
            print "Can't stat file '$filename': $!";
            next;
        }
        print "Concerning file '$filename':\n";
        print "  it's readable\n" if -r _;
        print "  it's writable\n" if -w _;
        ...
        print "\n";
    }

Hope this helps!

--
Tom Phoenix           http://www.teleport.com/~rootbeer/

Randal Schwartz Case:  http://www.rahul.net/jeffrey/ovs/
              Ask me about Perl trainings!



Fri, 19 May 2000 03:00:00 GMT  
 problem using eval with array of file test operators

Quote:

>Hi.  I am trying to write a method which prints out the status of a
>file using the file test operators.  The code is shown below.  I am
>having a problem trying to figure out how to set up the command text
>string and using eval.

When you print out the text you are about to eval, it should look
exactly like a script you would want to execute. Which probably means
interpolating $ftest, and not interpolating the variable names.

    $command =    
    "if ( $ftest \$fname ) {
      \$qualifier = "";
    } else {
      \$qualifier = "not";
    }";
    print "Command is $command\n";  
    eval $command ;

but instead of the eval, how about a couple of other options? If you
make the tests subroutine references instead of strings, you could
skip the eval.


                 [sub {-r $_[0]}, "File is %sreadable by effective uid\n" ],
# and the rest of the file tests.


  if(&{$ratest->[0]}($fname)) {
    $qualifier = '';
  }
  else {
    $qualifier = 'not ';
  }
  printf $ratest->[1], $qualifier;

Quote:
}

Also, most of the file mode tests are taken from the result of the
stat() command. You might just want to run stat() and pick apart the
results yourself.

--
Andrew Langmead



Fri, 19 May 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Test for eval() without using eval() ??

2. File test operators problem

3. File Test Operator Problem

4. Help with file test operator problem

5. Problems with File test operators (specifcally, -d)

6. Problems with file test operator (-d)

7. Problems reading file using <> operator

8. File test operators can't tell a file from a directory

9. File Test Operators and Speed

10. Wildcards allowed in file test operators?

11. help with file test operator

12. File Test Operators

 

 
Powered by phpBB® Forum Software