#!/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 ; } 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; } #### $ ./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 | '----+-----------------+-------' #### $ ./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 | '----+-----------------+-------' #### $ ./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 | '----+-----------------+-------' #### c:\cygwin64\home\ken\tmp\pm_11151991_io_select>perl no_woes.pl [1292] STDIN: TTY [1292] 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 | '----+-----------------+-------' c:\cygwin64\home\ken\tmp\pm_11151991_io_select>perl no_woes.pl | perl no_woes.pl [12492] STDIN: TTY [12492] STDOUT: PIPE [496] STDIN: PIPE [496] 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 | '----+-----------------+-------' c:\cygwin64\home\ken\tmp\pm_11151991_io_select>perl no_woes.pl | perl no_woes.pl | perl no_woes.pl [8268] STDIN: TTY [8268] STDOUT: PIPE [1576] STDIN: PIPE [1576] STDOUT: PIPE [3140] STDIN: PIPE [3140] 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 | '----+-----------------+-------' c:\cygwin64\home\ken\tmp\pm_11151991_io_select>perl no_woes.pl | perl no_woes.pl | perl no_woes.pl | perl no_woes.pl [11812] STDIN: TTY [11812] STDOUT: PIPE [12848] STDIN: PIPE [12848] STDOUT: PIPE [11704] STDIN: PIPE [11704] STDOUT: PIPE [2536] STDIN: PIPE [2536] 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 | '----+-----------------+-------'