in reply to File Test operators, option -T

You need to test the file, not the file handle.

my $file = 'input2.txt'; print "baba baba baba baba TEXTFILE!!!\n\n\n" if -T $file; # If you still want to read it, open the file handle and enjoy open my $fh, '<', $file or die "Can't open $file to read: $!\n";

Update

As hdb points out you can file test a file handle too. The OP was probably failing to open the file and thus getting a failure on the -T. This is another case highlighting the utility of the or die pattern

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re^2: File Test operators, option -T
by DanielSpaniel (Scribe) on Sep 10, 2013 at 13:40 UTC

    No, I think you're wrong. You DO want to test the file handle.

    With your code all you're testing is a variable value. Just because you put the name of a file in a variable doesn't make it a file - it's still just a variable.

    Actually, I've tested the poster's original code, testing with both binary and text files, and it works perfectly for me, on both Linux and Windows, so I'm not quite sure how he's even getting an error.

      Probably because the open failed if the file does not exist or is elsewhere. Testing for the success of open is a useful thing to do.

      And, -X accepts both the filehandle or the filename.

      Yes of course putting a string in a variable does not make it a file. Testing it with -T does check that it is a file, and it looks like text. As hdb points out the OPs open was probably failing so my addition of a test to open would have made that clear too.

      Cheers,
      R.

      Pereant, qui ante nos nostra dixerunt!