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
$SIG{INT} = \&interrupt; open LOGFILE, "|-", "tee some_file.out"; LOGFILE->autoflush(1); select LOGFILE; print "fileconsole!\n"; print "pause\n"; my $in = <STDIN>; select STDOUT; print "console1\n"; die; sub interrupt { print "Caught a control c!\n"; die; } END { select LOGFILE; if(fileno(LOGFILE)){ print "close logfile\n"; close(LOGFILE) or warn; } }
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
#! /usr/bin/env perl use strict; use warnings; sub interrupt { warn "CTRL+C detected.\n"; die; } sub ask_user { my $msg = shift; print $msg; chomp( my $input = <STDIN> || '' ); return $input; } $SIG{INT} = \&interrupt; open my $logh, '>', "/tmp/log.txt" or die "$!"; print $logh "Opened Logfile.\n"; while ( my $input = ask_user("Enter something: ") ) { print $logh $input, "\n"; } close $logh or die "$!"; END { return unless fileno $logh; print $logh "Close Logfile.\n"; close $logh or warn $!; }
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
$SIG{INT} = \&interrupt; open LOGFILE, "|-", "tee some_file.out"; LOGFILE->autoflush(1); select LOGFILE; print "fileconsole!\n"; print "pause\n"; my $in = <STDIN>; select STDOUT; print "console1\n"; die; sub interrupt { print "Caught a control c!\n"; die; } END { if(fileno(LOGFILE)){ print "close logfile\n"; close(LOGFILE) or warn; } }
1 2 3 4 5 6
sub mylog { my $msg = shift; print LOGFILE $msg or die "Cannot write to logfile: $!"; }
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
#! /usr/bin/env perl use strict; use warnings; $SIG{INT} = \&interrupt; #open LOGFILE, "|-", "tee some_file.out"; open LOGFILE, ">", "/tmp/log.txt"; LOGFILE->autoflush(1); select LOGFILE; print "fileconsole!\n"; print "pause\n"; my $in = <STDIN>; select STDOUT; print "console1\n"; die; sub interrupt { print "Caught a control c!\n"; die; } END { if(fileno(LOGFILE)){ print "close logfile\n"; close(LOGFILE) or warn; } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
### ORIGINAL
$ perl /tmp/t3.pl
fileconsole!
pause
^CDied at /tmp/t3.pl line 20.
141 $
### ERSETZUNG DURCH EINE LOGDATEI (siehe Code oben)
$ perl /tmp/t3.pl ; cat /tmp/log.txt
^CDied at /tmp/t3.pl line 21.
fileconsole!
pause
Caught a control c!
close logfile
0 $