Leser: 16
1 2 3 4 5 6 7 8
use meinpaket; if ($irgendwas) { my $datei = meinpaket->new('file'=>'bla.txt'); my $einpaarzeilen = $datei->get(4,7,8); $datei->set('5'=>'Neue fünfte Zeile'); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
package meinpaket; use strict; use Tie::File; my @a = (); sub new { my $c = shift; my $s = {}; my %p = @_; $s->{'file'} = $p{'file'}; tie @a, 'Tie::File', $s->{'file'}, memory=>10000 or return 0; $s->{a} = \@a; bless $s, $c; return $s; } sub DESTROY { my $s = shift; untie @{$s->{a}}; } sub get { my $s = shift; my @r = (); foreach (@_) { push @r, ${$s->{a}}[$_]; return \@r; } sub set { my $s = shift; my %p = @_; foreach (keys %p) { ${$s->{a}}[$_] = $p{$_}; } return 1; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
sub get { my( $self, @pos ) = @_; #return @{ $self->{'a'} }[ @pos ]; return @{ $self->{'a'} }[ map $_-1, @pos ]; } sub set { my( $self, %lines ) = @_; my @pos = keys %lines; #@{ $self->{'a'} }[ @pos ] = @lines{ @pos }; @{ $self->{'a'} }[ map $_-1, @pos ] = @lines{ @pos }; return $self; }
1 2
open my $fh, '<', $self->{'file'} or die "open: $!"; flock(LOCK_EX | LOCK_NB) or die "lock: $!";