1Size(3) User Contributed Perl Documentation Size(3)
2
3
4
6 Term::Size - Retrieve terminal size (Unix version)
7
9 use Term::Size;
10
11 ($columns, $rows) = Term::Size::chars *STDOUT{IO};
12 ($x, $y) = Term::Size::pixels;
13
15 Term::Size is a Perl module which provides a straightforward way to
16 retrieve the terminal size.
17
18 Both functions take an optional filehandle argument, which defaults to
19 *STDIN{IO}. They both return a list of two values, which are the
20 current width and height, respectively, of the terminal associated with
21 the specified filehandle.
22
23 "Term::Size::chars" returns the size in units of characters, whereas
24 "Term::Size::pixels" uses units of pixels.
25
26 In a scalar context, both functions return the first element of the
27 list, that is, the terminal width.
28
29 The functions may be imported.
30
31 If you need to pass a filehandle to either of the Term::Size functions,
32 beware that the *STDOUT{IO} syntax is only supported in Perl 5.004 and
33 later. If you have an earlier version of Perl, or are interested in
34 backwards compatibility, use *STDOUT instead.
35
37 1. Refuse to run in a too narrow window.
38
39 use Term::Size;
40
41 die "Need 80 column screen" if Term::Size::chars *STDOUT{IO} < 80;
42
43 2. Track window size changes.
44
45 use Term::Size 'chars';
46
47 my $changed = 1;
48
49 while (1) {
50 local $SIG{'WINCH'} = sub { $changed = 1 };
51
52 if ($changed) {
53 ($cols, $rows) = chars;
54 # Redraw, or whatever.
55 $changed = 0;
56 }
57 }
58
60 If there is an error, both functions return "undef" in scalar context,
61 or an empty list in list context.
62
63 If the terminal size information is not available, the functions will
64 normally return "(0, 0)", but this depends on your system. On
65 character only terminals, "pixels" will normally return "(0, 0)".
66
68 Term::Size only works on Unix systems, as it relies on the "ioctl"
69 function to retrieve the terminal size. If you need terminal size in
70 Windows, see Term::Size::Win32.
71
72 Before version 0.208, "chars" and "pixels" used to return false on
73 error.
74
76 Term::Size::Any, Term::Size::Perl, Term::Size::ReadKey,
77 Term::Size::Win32.
78
80 Tim Goodwin, <tim@uunet.pipex.com>, 1997-04-23.
81
83 Adriano Ferreira, <ferreira@cpan.org>, 2006-05-19.
84
85
86
87perl v5.30.1 2020-01-30 Size(3)