Size(3pm)

πŸ’– NAME πŸ’–

Term::Size β€” bestie, it gets you the terminal size on Unix ✨


πŸ¦‹ SYNOPSIS πŸ¦‹

use Term::Size;

($columns, $rows) = Term::Size::chars *STDOUT{IO};
($x, $y) = Term::Size::pixels;

πŸŽ€ DESCRIPTION πŸŽ€

Okay so Term::Size is a Perl module whose whole entire job is: tell you how big your terminal is. Straightforward. No drama. That's it. πŸ’…

Both functions take an optional filehandle argument, and if you don't hand one over, it defaults to *STDIN{IO}. And both of them return a list of two values β€” the current width and height (in that order, bestie, width first) of the terminal associated with the filehandle you gave it.

Term::Size::chars gives you the size in units of characters, while Term::Size::pixels is out here using units of pixels.

If you call either one in scalar context, you get the first element of that list β€” which is the terminal width. Just the width. That's the whole vibe.

The functions may be imported, so go off. 🌸

Now, real talk: if you need to pass a filehandle to either of the Term::Size functions, beware β€” the *STDOUT{IO} syntax is only supported in Perl 5.004 and later. So if you're on an earlier version of Perl, or you're out here caring about backwards compatibility, use *STDOUT instead. Noted. Important. Don't skip this one. 🫢


πŸ”₯ EXAMPLES πŸ”₯

  1. Refuse to run in a too narrow window.
use Term::Size;

die "Need 80 column screen" if Term::Size::chars *STDOUT{IO} < 80;
  1. Track window size changes.
use Term::Size 'chars';

my $changed = 1;

while (1) {
        local $SIG{'WINCH'} = sub { $changed = 1 };

        if ($changed) {
                ($cols, $rows) = chars;
                # Redraw, or whatever.
                $changed = 0;
        }
}

✨ RETURN VALUES ✨

If there's an error, both functions return undef in scalar context, or an empty list in list context. That's the "we have nothing for you bestie" response. 😭

If the terminal size information is just not available, the functions will normally return (0, 0) β€” but lowkey this depends on your system. And on character only terminals, pixels will normally return (0, 0). That's not a bug, it's just pixels not being a thing there. πŸ¦‹


πŸ’€ CAVEATS πŸ’€

Term::Size only works on Unix systems, because it relies on the ioctl function to retrieve the terminal size. Unix only. If you need terminal size on Windows, go see Term::Size::Win32.

Also, not me bringing up ancient history, but: before version 0.208, chars and pixels used to return false on error. So if you're digging through old code and something looks weird, that's why. πŸ™ˆ


🌈 SEE ALSO 🌈

  • Term::Size::Any
  • Term::Size::Perl
  • Term::Size::ReadKey
  • Term::Size::Win32

πŸ‘‘ AUTHOR πŸ‘‘

Tim Goodwin, <tim@uunet.pipex.com>, 1997-04-23.


πŸ’œ MANTAINER πŸ’œ

Adriano Ferreira, <ferreira@cpan.org>, 2006-05-19.


Copyright (C) 1997-05-13 by Tim Goodwin.

You may redistribute them under the same terms as Perl itself.


and that's the whole page, bestie ✨ this absolute icon of a page ships with perl v5.42.3, dated 2026-08-19 β€” mwah, periodt πŸ’…πŸŽ€

πŸ“¦ where this page comes from