#!/usr/bin/perl use strict; use warnings; # Paket zum kopieren aller Ein/Ausgaben {package PerlIO::via::mycopy; my $copyfile=undef; sub copyfile { my $f=shift(); # ignoriere die Übergeben Klasse oder Objekt $f=shift() if(defined($f) && (ref($f) eq __PACKAGE__ || $f eq __PACKAGE__) ); $copyfile = $f if(defined $f); return $copyfile; } sub PUSHED { my ($class,$mode,$fh)=@_; my $cf=$copyfile; return -1 unless(defined($cf)); if(ref($cf) ne 'GLOB') { $cf=undef; open($cf, '>', $copyfile) || return -1; } print $cf "Start Log at ".localtime()."\n"; return bless($cf,$class); } sub FILL { my ($myfh,$fh)=@_; if (defined( my $line=readline($fh) )) { print $myfh "IN: $line"; return $line; } return undef; } sub WRITE { my ($myfh,$buf,$fh)=@_; print $myfh "OUT: $buf"; return -1 unless(print $fh $buf); return length($buf); } 1;} package main; # entweder ein Filehande #open(my $log, '>', 'log.txt') or die "Error open Log ($!)\n"; #PerlIO::via::mycopy->copyfile($log); # oder ein Dateiname #PerlIO::via::mycopy->copyfile('log.txt'); # nach STDERR PerlIO::via::mycopy->copyfile(\*STDERR); { # STDOUT über mycopy umleiten open(my $stdout, ">&STDOUT"); close(STDOUT); open (STDOUT, ">&:via(mycopy)", $stdout) or die "Unable to mycopy standard output: $!\n"; } print "TEST1\n"; print "TEST2\n"; print "TEST3\n";