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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.