open file error

E

Emanuele Matli

Hi all,

I have a, probably, stupid problem, but I'm new to perl (not to
programming) and I can't see where the problem is.

A simple program containing this command:

open(FH, "test.txt") || die $!;

gives the following answer:

"No such file or directory"

The file is there and the permissions on the sever dir are ok.
On the other hand, if I try to open a file to write I get a permission
error because I don't have the right to write in the same dir.

Thanks.
 
B

Brian Gough

Emanuele Matli said:
The file is there and the permissions on the sever dir are ok.
On the other hand, if I try to open a file to write I get a permission
error because I don't have the right to write in the same dir.

Use open(FH, "<test.txt") to open the file read-only.
By default, open will access the file in read-write mode.
 
T

Tore Aursand

A simple program containing this command:

open(FH, "test.txt") || die $!;

gives the following answer:

"No such file or directory"

The file is there and the permissions on the sever dir are ok.

Can't be. Try to open the file using its full path, e.g:

open( FH, '/path/to/file/test.txt' ) or die "$!\n";

And - BTW: Don't use double quotes if you really don't have to. In this
case, you don't have to.
 
T

Tad McClellan

Emanuele Matli said:
I have a, probably, stupid problem, but I'm new to perl (not to
programming) and I can't see where the problem is.

A simple program containing this command:

open(FH, "test.txt") || die $!;

gives the following answer:

"No such file or directory"

The file is there


You would not get that message if the file was really "there".

Machines don't make mistakes. People do. :)

and the permissions on the sever dir are ok.


Server? What server?

No server is required to run Perl programs.

This must be a stealth-CGI question.

Is your Perl program running in a CGI environment perchance?



I'll guess that "there" (the cwd) is not where you think it is.

It is probably some directory other than where the file is.

Either chdir() to where the file is, or use an absolute, rather
than relative, path to the file.
 
T

Tintin

Emanuele Matli said:
Hi all,

I have a, probably, stupid problem, but I'm new to perl (not to
programming) and I can't see where the problem is.

It's not specific to Perl, so I'm surprised that you say you have
programming experience and can't narrow down the issue.
A simple program containing this command:

open(FH, "test.txt") || die $!;

gives the following answer:

"No such file or directory"

As it will when it can't find the file or directory.
The file is there and the permissions on the sever dir are ok.
On the other hand, if I try to open a file to write I get a permission
error because I don't have the right to write in the same dir.

The file is somewhere, but it certainly isn't where you run the script on.

Add

use Cwd;
print "My current directory is: " . cwd() . "\n";

That should give you a *big* clue.

Moral of the story is don't make assumptions about your current dir in a CGI
environment (which I'm sure this question is about) and use absolute paths
to make absolutely sure you point to the right location.
 
B

Brian Gough

Tad McClellan said:
Where in the std docs did you see it say that?

Sorry, it was a totally incoherent error due to lack of coffee, or
something... I cancelled the message immediately after :-(
 
E

Emanuele Matli

Tintin said:
It's not specific to Perl, so I'm surprised that you say you have
programming experience and can't narrow down the issue.

I agree I could have spent some time more to solve the problem by myself...
As it will when it can't find the file or directory.




The file is somewhere, but it certainly isn't where you run the script on.

Add

use Cwd;
print "My current directory is: " . cwd() . "\n";

Thanks for the help. The cwd() function was exactly what I was looking
for. I was trying to do the same thing with the url stuffs.
Unfortunately, since I was running the script from the server, the
"wwwroot" part of the path was not visible that way.
That should give you a *big* clue.

Moral of the story is don't make assumptions about your current dir in a CGI
environment (which I'm sure this question is about) and use absolute paths
to make absolutely sure you point to the right location.
Yes, I assumed that if the script and file are in the same dir no
relative path was necessary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top