B
bill
I run into the need to do this *all* the time, but I have not come
up with a solution. This need arises almost invariably during the
debugging of a CGI script. At the beginning of the run I want to
inspect all the input that the CGI script will read from STDIN,
but I want to then "restore" this input back to the STDIN stream
so that execution/debugging session can proceed normally.
I *think* the best solution would be to slurp all the text into a
lexical
my $input = do { local $/; <STDIN> };
# {
# open my $out, ">debugging_data" or die "$!\n";
# print $out $input;
# close $out or die "$!\n";
# }
associate this lexical with an IO::Scalar handle
my $SH = IO::Scalar->new(\$input);
and then redefine STDIN so that it points to this IO::Scalar.
*This* is the part that I just can't figure out. Following what
I see in tutorials and books, I have tried
open(STDIN, "<&$SH") or die "$!\n";
but the open fails with an "invalid argument" error message. I've
also tried
open(STDIN, "<&=$SH") or die "$!\n";
(which fails with the same error), and
*STDIN = *SH;
and many variants thereof, all of which fail silently.
It is obvious that I have *no clue* and am just wildly trying out
random stuff.
I've read every bit of documentation I can think of, but I have
not hit on the answer to this question. I know this is all very
basic, but if someone could spell it out for me I'd greatly appreciate
it.
bill
up with a solution. This need arises almost invariably during the
debugging of a CGI script. At the beginning of the run I want to
inspect all the input that the CGI script will read from STDIN,
but I want to then "restore" this input back to the STDIN stream
so that execution/debugging session can proceed normally.
I *think* the best solution would be to slurp all the text into a
lexical
my $input = do { local $/; <STDIN> };
# {
# open my $out, ">debugging_data" or die "$!\n";
# print $out $input;
# close $out or die "$!\n";
# }
associate this lexical with an IO::Scalar handle
my $SH = IO::Scalar->new(\$input);
and then redefine STDIN so that it points to this IO::Scalar.
*This* is the part that I just can't figure out. Following what
I see in tutorials and books, I have tried
open(STDIN, "<&$SH") or die "$!\n";
but the open fails with an "invalid argument" error message. I've
also tried
open(STDIN, "<&=$SH") or die "$!\n";
(which fails with the same error), and
*STDIN = *SH;
and many variants thereof, all of which fail silently.
It is obvious that I have *no clue* and am just wildly trying out
random stuff.
I've read every bit of documentation I can think of, but I have
not hit on the answer to this question. I know this is all very
basic, but if someone could spell it out for me I'd greatly appreciate
it.
bill