Danke für die Anregungen.
Auf die Idee den Rückgabewert von print $bytes zu prüfen bin ich nicht gekommen, der funktioniert sehr schön.
Ich habe allerdings gleich mal alle Vorschläge getestet (will ja auch lernen). Der Code sah dann so aus:
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
55
56
57
58
59
use strict;
$SIG{PIPE}=\&remove;
open(my $file, ">backup.txt");
for (my $i=1;$i<10000000;$i++) {
print $file "Hello World\r\n";
}
close($file);
my $filedestruct=new SQLDump('backup.txt');
print qq(Content-Type: text/plain\n);
print qq(Content-Disposition: attachment; filename="backup.txt"\n\n);
open(my $file, "<backup.txt");
binmode($file);
eval {
while (read($file,my $bytes,1024)) {
print $bytes or die 'STDOUT closed';
}
1;
} or do {
&remove("eval $@");
};
close($file);
&remove('normal');
sub remove {
&log;
my $filename='backup.txt';
if (-e $filename) {unlink($filename);}
}
sub log {
open(my $file, ">>log.txt");
print $file time.": @_\n";
close($file);
}
package SQLDump;
sub new {
my $self={filename=>$_[1]};
bless $self;
return $self;
}
sub DESTROY {
my $self=shift;
my $filename=$$self{filename};
&main::remove('destroy');
}
Wir Ihr seht, steht in der log.txt dann drin, was passiert ist.
Wenn man den Download vorzeitig abbricht steht da (auch in dieser Reihenfolge):
1351164942: PIPE
1351164942: eval STDOUT closed at ... line 22.
Wenn der Download durchläuft:
1351165285: normal
1351165285: destroy
Die SIG{INT} Variante kommt nie.
Das mit dem Destructor funktioniert leider auch nicht.
Ein interessanter Aspekt ist noch, wenn ich den eval-Block nicht selbst mit "die" verlasse, dann bekomme ich in der log.txt ganz viele PIPE-Signals auf einmal, und sonst nichts.
Falls noch jemand weitere Anregungen hat, dann sind diese herzlich wollkommen. Ansonsten werde ich jetzt einen eval-Block drumrumbasteln. Man da hätte ich auch selber drauf kommen können.