Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

what is the purpose of Tie::File

by Lotus1 (Curate)
on Nov 10, 2012 at 18:04 UTC ( #1003271=perlquestion: print w/replies, xml ) Need Help??
Lotus1 has asked for the wisdom of the Perl Monks concerning the following question:

Recently I saw code solutions posted to perlmonks that used Tie::File to read a file but all output was sent to an output file. This didn't make sense to me. Then I saw other monks comment that Tie::File is slow for changing very large files so I read about how it buffers records it reads and writes. I didn't understand what the module would be good for.

This morning I found a blog entry that states that Tie::File "is a delightful way to do in place editing of files." This clicked for me and seems obvious now. Now I see in the synopsis for the module a comment about editing in place. What are others using this module for and how useful do you find it?

Replies are listed 'Best First'.
Re: what is the purpose of Tie::File
by afoken (Abbot) on Nov 10, 2012 at 19:23 UTC

    Did you even try to read the documentation of Tie::File?

    Tie::File represents a regular text file as a Perl array. Each element in the array corresponds to a record in the file. The first line of the file is element 0 of the array; the second line is element 1, and so on.

    The file is not loaded into memory, so this will work even for gigantic files.

    Changes to the array are reflected in the file immediately.

    Lazy people and beginners may now stop reading the manual.

    Tie::File has its uses, but of course it introduces some overhead. For some manipulations, the overhead can become a real performance killer.


    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Did you even try to read the documentation...

      This question tells me you didn't even try to read what I posted, just the title. It probably wasn't the best title for this node but it was the question I had been wondering until this morning.

Re: what is the purpose of Tie::File
by Anonymous Monk on Nov 11, 2012 at 08:44 UTC

      Thanks for the links. Those make it pretty clear how it is useful. Although Perl isn't a text editor I seem to be too often.

      On one of the slides in the second link I found this interesting regex. s/^/>> /; Another obvious thing I suppose that I didn't know or realize before: that you can substitute something for nothing and therefore insert something at a boundary.

        Yes, or you can substitute something for what may be nothing, as in s/\n*\z/\n/.
        A math joke: r = | |csc(θ)|+|sec(θ)|-||csc(θ)|-|sec(θ)|| |
Re: what is the purpose of Tie::File
by remiah (Hermit) on Nov 12, 2012 at 00:32 UTC


    In the thread which reports UTF8 problem of Tie::File, I vaguely imagined a case which sometimes asked in this monk. That is "there is large, large file, and I want to find last 5 records and modify them".

    Tie::File, and DB_File could iterate from last records(because it is array), so I thought these could do that kind of jobs faster.

    Now I am thinking is there widgets (TK or GTK), using tied array feature? Say, $That_Widget::DATA[0]="new data"; updates file. Or, DBI supports this kind of data access like Tie::File ??? I wonder whether I responded your post ...

Re: what is the purpose of Tie::File
by Khen1950fx (Canon) on Nov 11, 2012 at 05:29 UTC
    Having a glitch of some kind---code is coming:). Here:
    #!/usr.bin/perl -l BEGIN { $| = 1; $^W = 1; } use strict; use autodie; use warnings; my $file = "file.txt"; 1 while unlink $file; use Tie::File; my $o = tie my @lines, 'Tie::Tile', $file, memory => 0; for (@lines) { $lines[2] = "something else is here now'; print $lines[2]; #line 3 is now "something else is here now". } END { undef $o; untie @lines; 1 while unlink $file; #clears and empties everything. }
      • Using perl -l gives you autochomp which Tie::File has built in. From your other nodes I see you use this frequently to add a newline when you print.
      • $^W = 1; and then use Warnings; a few lines later?
      • Using autodie means the script stops after the unlink line even if there is a file to delete. Why not just die if -e $file; ?
      • Does $| = 1; have any effect on Tie::File? With the option memory => 0 it is already not cacheing anything.

      I don't understand the point of this code. Tying to a file that doesn't exist, adding a line, then deleting the newly created file doesn't seem useful.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003271]
Approved by frozenwithjoy
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2017-12-14 15:24 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (396 votes). Check out past polls.