in reply to Re^6: Loading a part of the file to array using Tie::File
in thread Loading a part of the file to array using Tie::File

Except it's just as easy to load the file into memory and write it back out when it's that small.

By "ease of implementation" I meant:

open my $ifh, '<', $file or die $!; chomp( my @a = <$ifh> ); close $ifh; # vs. use Tie::File; tie my @a, 'Tie::File', $file; # and open my $ofh, '>', $file or die $!; print $ofh $_,"\n" for @a; close $ofh; # vs. untie @a;

I think several arguments, some subjective, could be made in either direction which one "easier". Whether it's worth the cost (speed and memory penalty) depends on the application, and of course the user must be aware of the penalty in the first place, so it's definitely good to caution. I just personally wouldn't reject the module outright.

Granted, if it's about reducing the length of the code, it's also possible to do something like this, although it still has a speed penalty, just not quite as bad as Tie::File:

use Path::Class qw/file/; my @a = file($file)->slurp(chomp=>1); ... file($file)->spew_lines(\@a);

A while(<>) should still be faster than any of the above. (I ran some quick back-of-the-envelope benchmarks with /usr/share/dict/words.)

Karl asked about "serious" use cases. Under the conditions I named, I'd say it's just a matter of TIMTOWTDI, but if you were to say that's not a "serious" use case, then you wouldn't be wrong. I think I'll be cautioning against Tie::File more in the future.

Replies are listed 'Best First'.
Re^8: Loading a part of the file to array using Tie::File
by ikegami (Patriarch) on Nov 26, 2017 at 07:51 UTC

    The fact that you can't even use Tie::File correctly says everything: The non-Tie implementation is clearly the easier of those two implementations.

    Aside the fact that your Tie::File code only saved you one line (after getting rid of those closes) and that replaced well-known operations with obscure ones, you forgot to throw an error for non-existing files in your Tie::File solution, and you hid the fact that you have to use a bunch of encodes and decodes with Tie::File while the alternative automatically uses the one encoding you set using use open.

      You are correct on both points, although I didn't intentionally hide anything - most of the data I work with day to day is plain ASCII, so I just missed the encoding issue (and Tie::File creates nonexistent files and I was testing in /tmp - but without or die it fails silently on e.g. permission denied, so that's not an excuse).

      use Tie::File; use Fcntl qw/O_RDWR/; tie my @a, 'Tie::File', $file, mode=>O_RDWR or die $!; # but doesn't handle encodings # - or - use Tie::File; open my $fh, '+<:encoding(UTF-8)', $file or die $!; tie my @a, 'Tie::File', $fh or die $!;

      Better examples, I hope. Thanks for setting me straight.