Fellow Monks,

I've been using Term::Size - and while hunting the problem described herein also Term::Size::Perl for detecting the size of the terminal my perl script is running in. I'm only interested in the chars (columns, rows). Everything woks fine, when you start the script in a shell: Both columns and rows are displayed correctly by both aforementioned modules.

Unfortunately, both modules fail if you have your script started from within another script through a pipe:

if (open my $cmdh, "$rcmd |") { ...
$rcmd being the name of the script that shall detect the terminal size. Unfortunately in this case, both modules return empty values for columns and rows. I'm using
my ($col, $row) = Term::Size::chars *STDOUT{IO};
to get the terminal dimensions.

Now that's of course totally unsatisfying and horrible and makes me whine. :-) Is there any way, how to pass terminal size elegantly to the piped command. (By elegantly I mean not the obvious way of putting these data on the command line - it should be transparent for the user).

edit:
Perlmonks rocks :-)
From the answers below, I've done a combined version

my ($col, $row); if (open my $tty, '<', '/dev/tty') { ($col, $row) = Term::Size::chars (defined $tty ? $tty: *STDIN{IO}) +; close $tty; }
Which seems to work pretty well in all cases where the old (STDOUT) worked before PLUS in those where it didn't. Also, this is able to cope with the </dev/null redirection.

Bye
 PetaMem
    All Perl:   MT, NLP, NLU


In reply to "Exporting" Terminal Size to Piped Command? by PetaMem

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.