in reply to simple message board

That'd be prepending, actually. The Tie::File module on the CPAN really the easiest way to accomplish this.

Replies are listed 'Best First'.
Re: Re: simple message board
by pg (Canon) on Oct 24, 2003 at 22:53 UTC

    This sounds like a reasonable solution at the beginning, but once you examined what Tie::File does in this case, the solution is quickly invalidated.

    Try this piece of testing code:

    use Tie::File; use Data::Dumper; my $self = tie @array, "Tie::File", "a.pl"; print Dumper($self); unshift @array, "abcd"; print Dumper($self);

    If you look at the offset array in the dump, you will see that the entire file is loaded to memory (not at a given point, but across the time line)in order to archieve this, which is really a waste, so this task should be rather handed over to OS.

    This is the test result against a copy of the demo program:

    $VAR1 = bless( { 'autochomp' => 1, 'mode' => 258, 'deferred_s' => 0, 'autodefer_threshhold' => 3, 'sawlastrec' => undef, 'defer' => 0, 'dw_size' => 2097152, 'offsets' => [ 0 ], 'deferred_max' => -1, 'autodeferring' => 0, 'recsep' => ' ', 'rdonly' => '', 'memory' => 2097152, 'filename' => 'a.pl', 'fh' => \*Tie::File::FH, 'ad_history' => [], 'autodefer' => 1, 'deferred' => {}, 'autodefer_filelen_threshhold' => 65536, 'recseplen' => 2, 'cache' => bless( [ bless( [ [ 0, $VAR1->{'cache'}, 0 ] ], 'Tie::File::Heap' ), {}, 2097152, 0 ], 'Tie::File::Cache' ) }, 'Tie::File' ); $VAR1 = bless( { 'autochomp' => 1, 'mode' => 258, 'deferred_s' => 0, 'autodefer_threshhold' => 3, 'sawlastrec' => undef, 'defer' => 0, 'dw_size' => 2097152, 'offsets' => [ 0, 6, 22, 41, 43, 88, 110, 135, 157 ], 'deferred_max' => -1, 'autodeferring' => 0, 'recsep' => ' ', 'rdonly' => '', 'memory' => 2097152, 'filename' => 'a.pl', 'eof' => 1, 'fh' => \*Tie::File::FH, 'ad_history' => [], 'autodefer' => 1, 'deferred' => {}, 'autodefer_filelen_threshhold' => 65536, 'recseplen' => 2, 'cache' => bless( [ bless( [ [ 0, $VAR1->{'cache'}, 0 ] ], 'Tie::File::Heap' ), {}, 2097152, 0 ], 'Tie::File::Cache' ) }, 'Tie::File' );

      So what?

      No, seriously. *Something* has to copy the whole file at some point. I don't know of any mechanism that can avoid that without mucking about with inodes. I personally would only do that as a lark and I wouldn't recommend it as a serious solution to anyone asking this question.

      Tie::File is well-maintained, has a nice interface, and is dead-simple to explain. If the original poster starts getting 86400 new messages per day (one hit per second), I'd start worrying about scalability.

      I'm okay with "wasting" a few dozen milliseconds and kilobytes here and there if it makes my life easier. If that's unreasonable, so be it.