1

I have a perl script on CentOS and am trying to read a file using File::Slurp:

my $local_filelist = '~/filelist.log';
use File::Slurp;
my @files = read_file($local_filelist);

But I get the following error:

Carp::croak('read_file \'~/filelist.log\' - sysopen: No such file or directory') called at /usr/local/share/perl5/File/Slurp.pm line 802

This is despite the fact that I am running the script as myuser and:

(2013-07-26 06:55:16 [myuser@mybox ~]$ ls -l ~/filelist.log
-rw-r--r--. 1 myuser myuser 63629044 Jul 24 22:18 /home/myuser/filelist.log

This is on perl 5.10.1 x86_64 on CentOS 6.4.

What could be causing this?

Kev
  • 15,899
  • 15
  • 79
  • 112

3 Answers3

5

I've not used File::Slurp, but I'll hazard a guess that it doesn't understand the ~ for home directory. Does it work if you specify the full path - e.g., use:

my $local_filelist = "$ENV{HOME}/filelist.log";

Using double quotes will mean that perl will expand $ENV{HOME}.

Chris J
  • 30,688
  • 6
  • 69
  • 111
3

Just use the glob function. That's what it is for.

my $local_filelist = glob '~/filelist.log';
daxim
  • 39,270
  • 4
  • 65
  • 132
2

I think that when you are running a script ~ may not be set or is set to somewhere else - try replacing:

'~/filelist.log'

with:

'/home/myuser/filelist.log'

in your script.

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73