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.

In reply to File::Slurp read_file error by patt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.