Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

what is the purpose of Tie::File

by Lotus1 (Vicar)
on Nov 10, 2012 at 18:04 UTC ( [id://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 (Chancellor) 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.

    Alexander

    --
    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

    Hello

    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 ...
    regards.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-24 10:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found