Created
December 10, 2015 08:38
-
-
Save davorg/4321043ca6e926c5eb84 to your computer and use it in GitHub Desktop.
Overriding CORE::GLOBAL::open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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