Leser: 15
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 39 40 41
use strict; use warnings; use Fcntl ':flock'; my $filename = shift || die "No filename!"; my $fh; open( $fh, "+<", "$filename" ) or die "Cant open $filename: $!"; flock( $fh, 0 ); my @daten = <$fh>; my ( $number, $date ); my $changed; for my $i ( 0 .. @daten - 1 ) { if ( $daten[$i] =~ /^Date:\s+\d{4}-\d\d?-\d\d?/ ) { my @datum = ( localtime(time) )[ 3 .. 5 ]; $daten[$i] = sprintf( "Date: %4d-%02d-%02d\n", $datum[2] + 1900, $datum[1] + 1, $datum[0] ); $changed++; } elsif ( $daten[$i] =~ /^Version:\s+\d+\.\d+\-\d+$/ ) { $daten[$i] =~ s/^(Version:\s+\d+\.\d+\-)(\d+)$/"$1".(1+$2)/e; $changed++; } } if ($changed) { seek( $fh, 0, 0 ) or die "Can't rewind $filename: $!\n"; truncate( $fh, 0 ); print $fh @daten; } close($fh) or die "Cant close $filename: $!"; __END__ Package: Packagename Date: 2012-01-10 Version: 0.3-1 Licence: GPL-3
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/perl
use strict;
use warnings;
sub print_file_content {
my $filename = $_[0];
my $fh;
open( $fh, "<", "$filename" ) or die "Cant open $filename: $!";
my @daten = <$fh>;
close($fh);
print "file $_[1] :\n";
for my $i ( 0 .. @daten - 1 ) {
if ($daten[$i] =~ /^Date:/ | $daten[$i] =~ /^Version:/) {
print " $daten[$i]";
}
}
}
use Fcntl ':flock';
my $filename = "candlesticks/pkg/DESCRIPTION";
print_file_content($filename, "before");
my $fh;
open( $fh, "+<", "$filename" ) or die "Cant open $filename: $!";
flock( $fh, 0 );
my @daten = <$fh>;
my ( $number, $date );
my $changed;
for my $i ( 0 .. @daten - 1 ) {
if ( $daten[$i] =~ /^Date:\s+\d{4}-\d\d?-\d\d?/ ) {
my @datum = ( localtime(time) )[ 3 .. 5 ];
$daten[$i] = sprintf(
"Date: %4d-%02d-%02d\n",
$datum[2] + 1900,
$datum[1] + 1,
$datum[0]
);
$changed++;
}
elsif ( $daten[$i] =~ /^Version:\s+\d+\.\d+\-\d+$/ ) {
$daten[$i] =~ s/^(Version:\s+\d+\.\d+\-)(\d+)$/"$1".(1+$2)/e;
$changed++;
}
}
if ($changed) {
seek( $fh, 0, 0 ) or die "Can't rewind $filename: $!\n";
truncate( $fh, 0 );
print $fh @daten;
}
close($fh) or die "Cant close $filename: $!";
print_file_content($filename, "after");
__END__