burnersk
burnersk

Reputation: 3480

How to read STDIN and write content back to STDIN in Perl?

I have to light patch some of the core components within an internal software product. Due to its realy nasty handling of STDIN I have to:

  1. read STDIN and backup RAW
  2. parse it
  3. write RAW back to STDIN
  4. let the downstream system handle STDIN

Unfortunally I am not allowed to modify any of the legacy code so I have to light patch it.

My first not working try:

BEGIN {
  my $stdin_raw = join '', <STDIN>;
  use IO::Handle;
  my $stdin_io1 = IO::Handle->new();
  $stdin_io1->printflush( $stdin_raw );
  my $stdin_io2 = IO::Handle->new();
  $stdin_io2->printflush( $stdin_raw );
  STDIN->fdopen( $stdin_io1, 'r' );
  require CGI;
  warn CGI::param('PARAM1');
  warn CGI::param('PARAM2');
  STDIN->fdopen( $stdin_io2, 'r' );
}

It's able to read STDIN in the first line but nighter CGI nor the downstream system are getting any input from STDIN which I (tried) to set as listed above.

So: How to read STDIN and write content back to STDIN in Perl?

The application is running under CGI (webserver) condition. STDIN handles the POST data of a web request.

Upvotes: 2

Views: 2679

Answers (2)

burnersk
burnersk

Reputation: 3480

Read, refill, read STDIN is not possible at all.

Upvotes: 0

ikegami
ikegami

Reputation: 385506

How about open(STDIN, '<', \$stdin_raw)


By the way,

my $stdin_raw = join '', <STDIN>;

is usually written as

my $stdin_raw; { local $/; $stdin_raw = <STDIN>; }

Probably more efficient too.

Upvotes: 2

Related Questions