Re: File Test operators, option -T
by Random_Walk (Prior) on Sep 10, 2013 at 10:24 UTC
|
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!
| [reply] [d/l] [select] |
|
|
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.
| [reply] |
|
|
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.
| [reply] [d/l] |
|
|
| [reply] |
Re: File Test operators, option -T
by choroba (Cardinal) on Sep 10, 2013 at 13:44 UTC
|
You should always check the return value of open. If the programme is not able to open the file, $fh will be undefined.
open my $fh, '<', 'input2.txt' or die $!;
| [reply] [d/l] |
Re: File Test operators, option -T
by Anonymous Monk on Sep 11, 2013 at 05:46 UTC
|
"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."
Thank you for reply. I should tell more clear that what error/warning I having in Perl version 5.10.1 in Centos LIVE disc
if I option -T with other options like -s -f
then I will get the error/warning
thank you | [reply] |
|
|
"if I option -T with other options like -s -f"
So, it sounds like the code you initially provided is not the same code as you're actually having a problem with. What is the exact code that you're having a problem with?
There is always the possibility, also, that your problem is related to using the live CD; I know that most things work as they would do on a real OS, but some things don't always work quite the same way (or at all).
| [reply] |
Re: File Test operators, option -T
by zork42 (Monk) on Sep 12, 2013 at 02:53 UTC
|
Do you have the following near the start of your script?
use strict; # essential
use warnings; # essential
use autodie; # optional
use diagnostics; # optional
The 2 "essentials" catch lots of errors and will save you lots of time. You should always use them!
If you'd had them in your script you'd have got this error message:
-T on closed filehandle $fh at script_name.pl line 2.
The 2 "optionals" can be very useful.
eg use autodie; would have given you this error message:
Can't open 'input2.txt' for reading: 'No such file or directory' at script_name.pl line 1
Documentation:
-
strict - Perl pragma to restrict unsafe constructs
-
warnings - Perl pragma to control optional warnings
-
autodie - Replace functions with ones that succeed or die with lexical scope
-
diagnostics - produce verbose warning diagnostics
| [reply] [d/l] [select] |
Re: File Test operators, option -T
by virtuemart2 (Novice) on Sep 12, 2013 at 10:04 UTC
|
I would like to point out that
the ordering of the options (like -f -s -T) MAY affect the logic/flow/something | [reply] |