Skip to content

Instantly share code, notes, and snippets.

@davorg
Created December 10, 2015 08:38
Show Gist options
  • Save davorg/4321043ca6e926c5eb84 to your computer and use it in GitHub Desktop.
Save davorg/4321043ca6e926c5eb84 to your computer and use it in GitHub Desktop.
Overriding CORE::GLOBAL::open
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
BEGIN {
no strict 'refs';
use Symbol ();
use Devel::Peek;
# See https://gist.github.com/ernix/a613c09d7e5fee7f91f9
*CORE::GLOBAL::open = sub (*;$@) {
warn "We're opening\n";
Dump($_[0]);
if (defined $_[0]) {
my $x = shift;
unshift @_,Symbol::qualify($x, scalar caller);
}
Dump($_[0]);
return CORE::open($_[0]) if @_ == 1;
return CORE::open($_[0], $_[1]) if @_ == 2;
return CORE::open($_[0], $_[1], $_[2]) if @_ == 3 && defined $_[2];
return CORE::open($_[0], $_[1], undef) if @_ == 3;
return CORE::open($_[0], $_[1], @_[2..$#_]);
};
}
package Foo::Bar;
sub opening {
open my $fh, '<', '/etc/passwd' or die $!;
say scalar <$fh>;
open FILEH, '</etc/passwd' or die $!;
say scalar <FILEH>;
}
package main;
Foo::Bar::opening();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment