You've commented out an essential statement :)
$_[0]=$handle unless defined $_[0]; # pass up to caller
This sets the $fh in open my $fh, ... Without it, $fh won't be the handle created in *CORE::GLOBAL::open.
Putting this near the end of that routine should suffice, but if you want the overridden open() to work for other forms too (not only 3-args open with lexical file handle), you should also re-enable the qualify_to_ref(...), i.e. the routine should then look like
*CORE::GLOBAL::open=sub (*;$@) { use Symbol(); my $handle; if (defined $_[0]) { $handle = Symbol::qualify_to_ref($_[0], scalar caller); } if (@_ == 1) { CORE::open $handle or warn $! and return 0; } elsif (@_ == 2) { CORE::open $handle, $_[1] or warn $! and return 0; } elsif (@_ == 3) { if (defined $_[2]) { CORE::open $handle, $_[1], $_[2] or warn $! and return 0; } else { CORE::open $handle, $_[1], undef # special case or warn $! and return 0; }; } else { CORE::open $handle, $_[1], $_[2], @_[3..$#_] or warn $! and return 0; }; $_[0]=$handle unless defined $_[0]; # pass up to caller tie *$handle,"TieInput",*$handle; };
With this change, I get the following output:
$ ./953722.pl opening in1.txt: TieInput: $_s = \'in1 A '; $s = \'in1 A '; at ./953722.pl line 107, <$In> line 1. TieInput: @_a = ( 'in1 B ', 'in1 C ' ); @a = ( 'in1 B ', 'in1 C ' ); at ./953722.pl line 109, <$In> line 3. opening in1.txt and in2.txt: TieInput: $_s = \'in1 A '; $s = \'in1 A '; at ./953722.pl line 118, <$In> line 1. TieInput: $_s = \'in2 A '; $s = \'in2 A '; at ./953722.pl line 120, <$In> line 1. TieInput: @_a = ( 'in1 B ', 'in1 C ' ); @a = ( 'in1 B ', 'in1 C ' ); at ./953722.pl line 122, <$In> line 3. TieInput: @_a = ( 'in2 B ', 'in2 C ' ); @a = ( 'in2 B ', 'in2 C ' ); at ./953722.pl line 124, <$In> line 3.
In reply to Re: Implicitly file tying ...
by Eliya
in thread Implicitly file tying ...
by clueless newbie
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |