Re: File opening problem

[email protected] ("Chas. Owens")
Newsgroups perl.beginners
Message-ID <[email protected]>
On May 12, 2008, at 09:05, Tatiana Lloret Iglesias wrote:

> Hi all!
>
> i'm running the following dummy program which just opens a file and  
> I get an
> error  (die message)
>
> #!/usr/bin/perl
>
>
> if (  @ARGV[0] eq '' )
> {
>   print "\nUSAGE:\n\t genenames.pl genes.txt \n\n";
>   exit;
> }
>
> my $file = $ARGV[0];
> open(FICH,"$file") or die "cannot open $file";
>
> I've tried to pass the input parameter ARGV[0] with / with \ with  
> relative
> path ... but nothing
>
> any idea?
>
> Thanks a lot!
> T

Your code looks like it should work, so  the only thing you can do is  
get perl to tell you why it can't open the file.  The $! variable  
contains the error number/message (depending on context) for system  
calls, so you can say

open my $fh, "<", $file
    or die "could not open $file: $!";

Note the usage of the three argument version of open.  It is safer  
than using the two argument version.  Also note the usage of lexical  
file handles.  The only bareword filehandles you should be using are  
STDIN, STDOUT, STDERR, and DATA.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.