Question

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?

Was it helpful?

Solution 2

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

my $local_filelist = glob '~/filelist.log';

OTHER TIPS

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}.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top