Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

"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
and cookies bake in the oven...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2018-02-24 01:06 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (310 votes). Check out past polls.