in reply to CORE::GLOBAL::* for auditing which files are read and written?

Tim:


Per the perldoc:
http://perldoc.perl.org/CORE.html
... you can globally override CORE::GLOBAL::open with your own function.

It was a bit harder to do than I though though...

BEGIN { *CORE::GLOBAL::open = sub (*;$@) { ($package, $filename, $line) = caller(); print "LOGGING: intercepted `open` call from ( package: '$package' +, file: '$filename', line: '$line' ) with args: '??', ".join(", ",map +{ "'$_'" } @_) . "\n"; if(defined($_[0])) { use Symbol qw(); my $handle = Symbol::qualify($_[0],(caller)[0]); no strict 'refs'; if(@_ == 1) { return CORE::open($handle); } elsif(@_ == 2) { return CORE::open($handle, $_[1]); } else { return CORE::open($handle, $_[1], @_[2..$#_]); } } else { if(@_ == 1) { return CORE::open($_[0]); } elsif(@_ == 2) { return CORE::open($_[0], $_[1]); } else { return CORE::open($_[0], $_[1], @_[2..$#_]); } } }; } open(my $fh, "<", ".bashrc") or die "unable to use hacked up open func +tion: $!"; my $top = <$fh>; print $top;

Which returns:
LOGGING: intercepted `open` call from ( package: 'main', file: './core +over.pl', line: '34' ) with args: '??', '', '<', '.bashrc' # ~/.bashrc: executed by bash(1) for non-login shells.
Note: i had done this before, but couldn't quite remember how to make it work with open($fh ..) instead of open(FH ..), so perl.com helped me out:
http://www.perl.com/pub/a/2002/06/11/threads.html?page=2
-- AlexLC