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


In reply to Re: IO::Select woes [Prime with NULL] by kcott
in thread IO::Select woes by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.