pHorseSpec
pHorseSpec

Reputation: 1274

Open File to Read from in Perl

I just started learning Perl today. I'm on the section of file input and output. This is a very basic question, and I've been searching on the internet for a couple of hours as to what I'm doing wrong, but I can't seem to find out why. I'm sure some of you think this question should be voted down, but if I could find the answer by myself through internet searching and troubleshooting, I wouldn't be asking it here.

My question is why can't I open the file that I'm referring to in my filepath?

open(my $in,  "<",  "ioFile.txt")  or die "Can't open input.txt: $!";

The ioFile.txt is in the same directory as my Perl script. I've used multiple different filepaths to see which worked, and none have for me so far. I've tried using forward slashes instead of backslashes as well.

Any tips about opening this specific file or files in general in Perl would be greatly appreciated.

After Edit:

It could be permissions on the file, but I do have read and write permissions on the file, but not full control permissions. I'm on Windows 7 btw.

Upvotes: 0

Views: 90

Answers (1)

stevieb
stevieb

Reputation: 9306

If you're not running the script while you are in the directory the script and the file you want to open are in, then you have to specify the full path to the file:

open my $in, '<', 'c:\path\to\ioFile.txt' or die "Can't open input.txt: $!";

perl will look for the input file from the location you are running the script from, not in the directory the script is in (again, unless you are in that directory when you are running the script).

Upvotes: 2

Related Questions