Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

"Exporting" Terminal Size to Piped Command?

by PetaMem (Priest)
on Jul 22, 2008 at 20:31 UTC ( #699419=perlquestion: print w/replies, xml ) Need Help??
PetaMem has asked for the wisdom of the Perl Monks concerning the following question:

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).

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.

    All Perl:   MT, NLP, NLU

Replies are listed 'Best First'.
Re: "Exporting" Terminal Size to Piped Command?
by almut (Canon) on Jul 22, 2008 at 21:38 UTC

    Works for me if I use STDIN or STDERR instead of STDOUT. Those handles are still connected to the terminal the parent script is started from1, while STDOUT (in the child) is connected to the pipe (for which the used TIOCGWINSZ ioctl call is invalid).


    1 unless you've reopened them to somewhere else in the Perl code, or redirected them to/from a file in the calling shell, of course...


    _____ _____

    #!/usr/bin/perl use Term::Size; my ($col, $row) = Term::Size::chars *STDIN{IO}; print "$col x $row\n";

    _____ _____

    #!/usr/bin/perl my $rcmd = "./"; if (open my $cmdh, "$rcmd |") { print <$cmdh>; }


    $ ./ 120 x 50 $ ./ </dev/null # doesn't work x
Re: "Exporting" Terminal Size to Piped Command?
by dave_the_m (Prior) on Jul 22, 2008 at 20:39 UTC
    Try using /dev/tty (untested):
    open my $tty, '<', '/dev/tty'; my ($col, $row) = Term::Size::chars (defined $tty ? $tty: *STDOUT{IO}) +;


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://699419]
Approved by moritz
[marto]: "Eugenie Scott, executive director of the National Center for Science Education, dubbed this approach the Gish gallop, describing it as "where the creationist is allowed to run on for 45 minutes or an hour, spewing forth torrents of error that the
[marto]: evolutionist hasn't a prayer of refuting in the format of a debate." She also criticized Gish for failing to answer objections raised by his opponents"
[erix]: one would hope evolutionists haven't any prayers anyway
[marto]: obviously someone could be religious, but not creationist
[erix]: "Nothing in Intelligent Design makes sense except in the light of Creationism" <-- I made that one up myself (free after Dobzhansky )
[erix]: yes. Deplorable marto, deplorable.
[marto]: the situation seemed similar to this one, majority of the contributrions are nonsense, doesn't address any questions ...
[marto]: meh, I've been called worse :P

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2017-07-28 15:43 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (431 votes). Check out past polls.