Re: open file failed
- From: japhy@xxxxxxxxxxxx (Jeff 'japhy' Pinyan)
- Date: Wed, 25 May 2005 21:28:56 -0400 (EDT)
On May 26, bingfeng zhao said:
On RedHat Linux, the perl complain "Inappropriate ioctl for device" when I use the following code to open a file:
my $file = "./abc";
if ( open FN, $file )
{
print "Cannot open the file: $!\n";
next;
}
Um, you're printing that message if the file DOES open! You want 'unless' instead of 'if', or else a 'not' operator in there:
if (not open FN, $file) { ... }or
unless (open FN, $file) { ... }The value in $! is unreliable if it hasn't actually been set (as is the case in your code).
I modified code and test the FH as following, perl indicate "Bareword "FH" not allowed while "strict subs"". <CODE> open FH, $name; if ( not defined FH )
You'd need to do *FH instead of FH, but that's really not the right way to go about doing this. If you wanted to do it based on the filehandle, you could do:
if (not defined fileno(FH)) { ... }But why not just write:
open(FH, $file) or do {
warn("can't read $file: $!");
next;
};That's nicer on the eyes, I think.
BTW, why a DOS-style perl file cannot run on linux and the bash report ": bad interpreter: No such file or directory"? It will be OK if I save the file as Unix-style and FTP to linux.
This is because if you transfer it incorrectly, the first line (the #! line) is actually '#!/usr/bin/perl\r\n' instead of '#!/usr/bin/perl\n' which means that Unix is looking for the program '/usr/bin/perl\r', and that file doesn't exist.
Another question is what is the simplest way to replace "\r\n" with "\r" or "\n"?
I think you'd just want to remove the \r's. This is how you remove \r's from a string in Perl:
$string =~ tr/\r//d;
Or, on Unix, use the dos2unix utility -- you might already have it.
-- Jeff "japhy" Pinyan % How can we ever be the sold short or RPI Acacia Brother #734 % the cheated, we who for every service http://japhy.perlmonk.org/ % have long ago been overpaid? http://www.perlmonks.org/ % -- Meister Eckhart .
- Prev by Date: open file failed
- Next by Date: Re: How to upgrade local module using -MCPAN?
- Previous by thread: open file failed
- Index(es):
Relevant Pages
|