Re: what is the purpose of Tie::File by afoken (Parson) 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". ;-)
| [reply] |
|
| [reply] |
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.
}
| [reply] [d/l] |
|
Having a glitch of some kind---code is coming:). Here: How delightfully refreshing, here's mine
P.S.
It now runs great!
The output is twice as much
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
| [reply] |
|
|
Re: what is the purpose of Tie::File by Anonymous Monk on Nov 11, 2012 at 08:44 UTC |
| [reply] |
|
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.
| [reply] [d/l] |
|
| [reply] [d/l] |
Re: what is the purpose of Tie::File by remiah (Friar) 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.
| [reply] |