in reply to IO::Select woes

If you send a NULL ("\0") to STDOUT, nothing happens with a standalone script, but it will be recognised as STDIN by a script to which it is piped.

I believe the following script (no_woes.pl) resolves all of the various issues that I've seen in this thread.

#!/usr/bin/env perl use strict; use warnings; use constant READ_TIMEOUT => 0.5; use Text::ASCIITable; use IO::Select; if (-t \*STDIN) { warn "[$$] STDIN: TTY\n"; } else { warn "[$$] STDIN: PIPE\n"; } if (-t \*STDOUT) { warn "[$$] STDOUT: TTY\n"; } else { warn "[$$] STDOUT: PIPE\n"; } $| = 1; print "\0" if -t \*STDIN; my $io_select = IO::Select->new(\*STDIN); if ($io_select->can_read(READ_TIMEOUT)) { print for <STDIN>; } else { print get_table(); } sub get_table { my $t = Text::ASCIITable::->new({ headingText => 'Basket' }); $t->setCols('Id', 'Name', 'Price'); $t->addRow(1, 'Dummy product 1', 24.4); $t->addRow(2, 'Dummy product 2', 21.2); $t->addRow(3, 'Dummy product 3', 12.3); $t->addRowLine(); $t->addRow('', 'Total', 57.9); return $t; }

Standalone output:

$ ./no_woes.pl [1564] STDIN: TTY [1564] STDOUT: TTY .------------------------------. | Basket | +----+-----------------+-------+ | Id | Name | Price | +----+-----------------+-------+ | 1 | Dummy product 1 | 24.4 | | 2 | Dummy product 2 | 21.2 | | 3 | Dummy product 3 | 12.3 | +----+-----------------+-------+ | | Total | 57.9 | '----+-----------------+-------'

Piped output:

$ ./no_woes.pl | ./no_woes.pl [1566] STDIN: PIPE [1566] STDOUT: TTY [1565] STDIN: TTY [1565] STDOUT: PIPE .------------------------------. | Basket | +----+-----------------+-------+ | Id | Name | Price | +----+-----------------+-------+ | 1 | Dummy product 1 | 24.4 | | 2 | Dummy product 2 | 21.2 | | 3 | Dummy product 3 | 12.3 | +----+-----------------+-------+ | | Total | 57.9 | '----+-----------------+-------'

Multiple pipes:

$ ./no_woes.pl | ./no_woes.pl | ./no_woes.pl [1568] STDIN: PIPE [1568] STDOUT: PIPE [1567] STDIN: TTY [1567] STDOUT: PIPE [1569] STDIN: PIPE [1569] STDOUT: TTY .------------------------------. | Basket | +----+-----------------+-------+ | Id | Name | Price | +----+-----------------+-------+ | 1 | Dummy product 1 | 24.4 | | 2 | Dummy product 2 | 21.2 | | 3 | Dummy product 3 | 12.3 | +----+-----------------+-------+ | | Total | 57.9 | '----+-----------------+-------' $ ./no_woes.pl | ./no_woes.pl | ./no_woes.pl | ./no_woes.pl [1571] STDIN: PIPE [1571] STDOUT: PIPE [1572] STDIN: PIPE [1572] STDOUT: PIPE [1573] STDIN: PIPE [1573] STDOUT: TTY [1570] STDIN: TTY [1570] STDOUT: PIPE .------------------------------. | Basket | +----+-----------------+-------+ | Id | Name | Price | +----+-----------------+-------+ | 1 | Dummy product 1 | 24.4 | | 2 | Dummy product 2 | 21.2 | | 3 | Dummy product 3 | 12.3 | +----+-----------------+-------+ | | Total | 57.9 | '----+-----------------+-------'

That was run on Cygwin: "perl 5, version 36, subversion 0 (v5.36.0) built for cygwin-thread-multi".

AM comment:

"Note: If you are on windows this will never work as select is only implemented for sockets, and not STDIN."

That same code works on Win10 (Strawberry Perl): "perl 5, version 26, subversion 3 (v5.26.3) built for MSWin32-x64-multi-thread". The output is very similar, so I've put it in a spoiler to avoid cluttering the thread.

— Ken