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"
In reply to Re^2: Override the open builtin globally
by Eliya
in thread Override the open builtin globally
by ninuzzo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |