Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by ikegami (Patriarch)
on Nov 24, 2017 at 21:52 UTC ( #1204210=note: print w/replies, xml ) Need Help??


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

Dunno, I think that for random access of small files (say, maybe, under a megabyte) in situations where performance is not critical, the ease of implementation can still outweigh the cost

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

  • Comment on Re^6: Loading a part of the file to array using Tie::File

Replies are listed 'Best First'.
Re^7: Loading a part of the file to array using Tie::File
by haukex (Bishop) on Nov 25, 2017 at 16:18 UTC
    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.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1204210]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2022-05-18 02:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (68 votes). Check out past polls.

    Notices?