Re: open file failed



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
.



Relevant Pages

  • Re: [Q] how to chech FTP login ???
    ... >> We just migrate out UNIX system to Redhat LINUX system. ... where is log file for FTP and sftp? ... > It's probably better to check the ftp server logs in /var/log anyway. ... Unix Guy Consulting, LLC ...
    (linux.redhat)
  • Re: How to start FTP service on my unix box......
    ... > I can connect ftp from my unix machine but from other machines when i ... > I am new to unix and don't know how to enable FTP service on a fresh ... UNIX or Linux or whatever you have running. ...
    (comp.unix.programmer)
  • Re: making a machine non pingable
    ... >> I know it can be done how do I make a unix box unpingable but still allow ... >> ftp and ssh access? ... In a Linux box this should work: ...
    (comp.security.unix)
  • Re: The Problems With Linux
    ... > Here are the problems with Linux: ... programmers of UNIX, Open Source software, and Shareware. ... programs only available from BSD. ... BSD code is used by Microsoft in it's Windows NT/9x/2K/XP code. ...
    (comp.os.linux)
  • RE: [Full-Disclosure] CyberInsecurity: The cost of Monopoly
    ... I've used UNIX, ... I've used/use Linux, ... > inefficient operating system - if most other Windows ... This is why I don't run POS operating systems. ...
    (Full-Disclosure)