patt has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monks, I'm seeking some insight into an error I am receiving when using File::Slurp's read_file. What I'm doing is reading all files and sub-directories in a directory into an array using ls -AR. Then I want to read each file in this array into a scalar for further processing, using File::Slurp. However the first file read_file hits it returns an error: read_file 'dir_name/file_name' - sysopen: No such file or directory at 'file_name' This first file is a shell script, executable, text file and it does exist. Following is an example of the code I'm using to test this.
my $a = 0; while (@fNameArray) { if(-d substr($fNameArray[$a], 1, -2)) { $dirName = substr($fNameArray[$a], 1, -2); #directory name is allocated to a scalar for reuse. }#end if elsif(! -d substr($fNameArray[$a], 1, -2)) { $data = read_file($dirName.'/'.substr($fNameArray[$a], 1, -1)); }#end elsif $a++; }#end while
The substr calls are necessary because when ls -AR returns its list of files and directories it will return directory names with a semi-colon and newline appended and file names are returned with a newline appended. These have to be removed also the directory name and file name have to be spliced with a / to give the read_file call a valid 'path/filename'. I assumed that read_file would read any file into a scalar, and I expected it to read a text file without problems. Can anyone please point out what I'm doing wrong? The file is processed as it is found - without a file name extension.

Replies are listed 'Best First'.
Re: File::Slurp read_file error
by moritz (Cardinal) on Aug 12, 2010 at 09:55 UTC
    Check the existence of the filename with something like this:
    my $fn = $dirName.'/'.substr($fNameArray[$a], 1, -1) print "File '$fn' exists: ", -e $fn ? "yes\n" : "no\n";

    I bet it does not. Maybe you're not in the directory where you think you are? Or maybe there are trailing spaces in your file name?

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: File::Slurp read_file error
by fod (Friar) on Aug 12, 2010 at 11:49 UTC
    I'm not sure your substr's are doing what you think they're doing. substr($string,1,-2) will remove the first character and the last two characters from a string. e.g. perl -e 'print substr("string",1,-2)' prints tri. Is this what you want to do? Or do you just want to remove the last two characters, in which case you could do substr($string,0,-2).

    Also, I'd be more inclined to remove newlines from file- and path-names with chomp while populating the array. This way you don't have to worry about accidentally removing characters you want to keep with substr. For instance (not sure if this is the way you're going about this), if I had a file produced by doing ls -AR directory_name > filelist in the shell I would put it into an array like so:

    use strict; use warnings; use autodie; my $file = shift @ARGV; open my $fh, '<', $file; my @files; while ( my $line = <$fh> ) { chomp $line; push @files, $line unless $line =~ /^\s*$/; }
    .. and I'd use File::Spec to assemble the pathnames so I didn't need to worry about concatenating pathnames and filenames and slashes and whether there was already a trailing slash in the direcory name, etc:
    use File::Spec; my $dirname; for my $name ( @files ) { if ( $name =~ s/:$// ) { # use trailing : to id dirname $dirname = $name; } else { my $fullpath = File::Spec->catfile( $dirname, $name ); # slurp or do whatever with file at $fullpath } }
      I tried chomp, but directory names had a ':' and \n to remove at the end and file names just a \n. I considered it more transparent for me to use the syntax I was using as I could track the directory and file processing easier.

      I also needed to keep a hierarchical list of dirs. and files in order to retrieve them from the directory tree, so push would not be efficient for me. I may need to work on approx. 500K+ files, so I need to reduce memory usage as much as I possibly can.

      substr($string,1,-2) as pointed out, was a typo on my part, should have read substr($string, 0, -2). Apologies for wasting your valuable time, but thank you.
Re: File::Slurp read_file error
by patt (Scribe) on Aug 12, 2010 at 11:38 UTC
    Thanks Moritz for your advice. The file did exist, but your suggestion reported that it did not. I have found the primary problem in my code. In the elsif test I did not have the pathname to the directory I was recovering files from, prefixed to fNameArray[a] so my code was not testing properly for directories. I have corrected that error and another syntax error I had. The code is executing now, as I intended it to. Thanks...

      patt:

      FYI: When replying to someone, be sure to reply to their node, or they may not see your reply. That may cause the conversation to end before the issue is resolved...

      ...roboticus

        Duly noted, that protocol will be followed. Thanks for pointing that out...