in reply to Re: Override the open builtin globally
in thread Override the open builtin globally
if(defined($_[0])) { ...
You forgot to handle the case when $_[0] is undef, i.e. when the form open my $fh, ... is being used.
BEGIN { *CORE::GLOBAL::open = sub (*;$@) { use Symbol (); my $handle = Symbol::qualify_to_ref($_[0], scalar caller); $_[0] = $handle unless defined $_[0]; # pass up to caller if (@_ == 1) { CORE::open $handle; } elsif (@_ == 2) { CORE::open $handle, $_[1]; } elsif (@_ == 3) { if (defined $_[2]) { CORE::open $handle, $_[1], $_[2]; } else { CORE::open $handle, $_[1], undef; # special case } } else { CORE::open $handle, $_[1], $_[2], @_[3..$#_]; } }; } # sample usage: open my $fh, ">", \my $buf or die $!; print $fh "hello"; print $buf; # prints "hello"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Override the open builtin globally
by Anonymous Monk on Jan 15, 2017 at 09:54 UTC | |
by choroba (Cardinal) on Jan 15, 2017 at 10:17 UTC | |
by Anonymous Monk on Jan 16, 2017 at 07:57 UTC |