http://qs321.pair.com?node_id=1204905

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

been frying my brain what was wrong with my code as it always fail when it parse the content of the file using tie::file. so i did subset of my code as below & as you can see, simple file test has no error but when i parse the file using tie::file, it throws an issue. please have a look & let me know if i missed anything critical here.

use strict; use warnings; use Tie::File; my $dir = "//folderA/folderB/folderC"; my $file = "//folderA/folderB/folderC/folderD/file.txt"; if (-d -e -r $dir){ print "Folder exists and readable.\n";} if (-f -e $file){ print "File exists and accessible.\n";} tie my @content, 'Tie::File', $file or die "Cannot open $file for read +ing."; Folder exists and accessible. File exists and accessible. Cannot open //folderA/folderB/folderC/folderD/file.txt for reading. at + te st.pl line 14.

Replies are listed 'Best First'.
Re: Tie::File with absolute path of the file
by tangent (Vicar) on Dec 05, 2017 at 01:22 UTC
    In addition to what toolic has advised, you can test if your file is readable and writeable using -r and -w. In its default mode Tie::File tries to open the file in read/write mode so the file needs to be both readable and writeable.

    If you only want to read the file you would use tie like this:

    use Tie::File; use Fcntl 'O_RDONLY'; #... tie (my @content, 'Tie::File', $file, mode => O_RDONLY) or die "Could +not open $file: $!";
      many thanks!.. this solved my issue.
Re: Tie::File with absolute path of the file
by toolic (Bishop) on Dec 05, 2017 at 01:11 UTC
    Tie::File can also write to the file. Maybe you don't have write permissions. Check the $! variable
    tie my @content, 'Tie::File', $file or die "$file: $!";

    Tip #7 from the Basic debugging checklist. Also, it looks strange to chain file test operators together like that. It might work, but I don't remember seeing it done that way.

      ... chain file test operators ...

      Per -X: "As of Perl 5.10.0, as a form of purely syntactic sugar, you can stack file test operators ... -f -w -x $file ..."


      Give a man a fish:  <%-{-{-{-<

Re: Tie::File with absolute path of the file
by haukex (Archbishop) on Dec 05, 2017 at 06:13 UTC

    In a recent thread I was sufficiently convinced by other monks that Tie::File - even though I still think it's kind of "neat" - is not a good choice in a serious application. On a large file it's much too inefficient, and even on a small file it's still more efficient to just read the entire file into an array and write it back out after modifying. So either:

    open my $ifh, '<', $filename or die "$filename: $!"; chomp( my @array = <$ifh> ); close $ifh; # modify @array here open my $ofh, '>', $filename or die "$filename: $!"; print $ofh $_,"\n" for @array; close $ofh;

    Or, to demonstrate a while(<>) loop and plug one of my own modules ;-) see File::Replace:

    use File::Replace; my $repl = File::Replace->new($filename); my $infh = $repl->in_fh; while (my $line = <$infh>) { chomp($line); # modify $line here print {$repl->out_fh} $line, "\n"; } $repl->finish;

    As for the problem you're having with Tie::File, I think both tangent and toolic have a very good point about write permissions. I would also add that perhaps using proper modules to handle file names platform independently might help with mitigating headaches about slashes etc. in filenames: either the core File::Spec, or, with a much nicer interface, Path::Class (there's also Path::Tiny with a similar interface, but be aware that that's restricted to Windows an *NIX).