0

I've tried to transfer my script over that I've been working on in Windows. But it fails at the first hurdle.

use strict;
use warnings;

my $keywordFile = 'keyword.txt';        

open(keyWords, $keywordFile) or die "$keywordFile not found\n";

my @keywordArray;

while ( my $line = <keyWords> ) {
    chomp $line;
    push @keywordArray, $line;
}

close(keyWords);

It keeps on dying, even though in the same destination there is a file called 'keyword.txt'. Is the issue coming from Ubuntu, or is my Perl wrong?

Borodin
  • 126,100
  • 9
  • 70
  • 144
Nafe
  • 11
  • 2
  • 5
    Add `$!` to your `die` message and [edit] the output into your question: `or die "Failed to open '$keywordFile': $!";` (note that file not found is not the only reason an `open` can fail). – ThisSuitIsBlackNot Jan 07 '16 at 15:34
  • `It keeps on dying`, where ? At `die "$keywordFile not found\n";` ? – 123 Jan 07 '16 at 15:35
  • 3
    Also, you should use lexical filehandles and the 3-arg form of open. See [Why are three-argument open calls with lexical filehandles a Perl best practice?](http://stackoverflow.com/q/1479741/176646) – ThisSuitIsBlackNot Jan 07 '16 at 15:44
  • 7
    The current directory probably isn't what you think it is. – ikegami Jan 07 '16 at 15:58

1 Answers1

0

It keeps on dying, even though in the same destination there is a file called 'keyword.txt'

Relative paths are resolved against working directory (i.e. the directory script is run from), not the directory in which script is located.

You should either

  1. Run your script from the directory that contains all needed files
  2. Use FindBin module to get script location and use that path to refer to file names:

eg.

use FinBin;
my $keywordFile = "$FindBin::Bin/keyword.txt";