Term::Choose(3pm)
π NAME π
Term::Choose β choose items from a list interactively. Yep, that's Term::Choose(3pm), a User Contributed Perl Documentation situation, and bestie it's about to be your new favorite module β¨
πΈ VERSION πΈ
Version 1.782 π
π¦ SYNOPSIS π¦
Okay so there's two whole ways to do this and both of them eat.
Functional interface:
use Term::Choose qw( choose );
my $array_ref = [ qw( one two three four five ) ];
my $choice = choose( $array_ref ); # single choice
print "$choice\n";
my @choices = choose( [ 1 .. 100 ], { alignment => 1 } ); # multiple choice
print "@choices\n";
choose( [ 'Press ENTER to continue' ], { prompt => '' } ); # no choice
Object-oriented interface:
use Term::Choose;
my $array_ref = [ qw( one two three four five ) ];
my $new = Term::Choose->new();
my $choice = $new->choose( $array_ref ); # single choice
print "$choice\n";
my @choices = $new->choose( [ 1 .. 100 ] ); # multiple choice
print "@choices\n";
my $stopp = Term::Choose->new( { prompt => '' } );
$stopp->choose( [ 'Press ENTER to continue' ] ); # no choice
π DESCRIPTION π
Choose interactively from a list of items. That's the whole vibe π«Ά
Term::Choose gives you a functional interface (SUBROUTINES) and an object-oriented interface (METHODS). Both. At once. Iconic behavior honestly.
π EXPORT π
Nothing by default. Not a single thing, she's shy like that π
use Term::Choose qw( choose );
So yeah β you gotta ask for choose, she's not gonna just show up.
π METHODS π
π new
$new = Term::Choose->new( \%options );
This constructor returns a new Term::Choose object. Brand new, fresh out the box π
To set the different options it can be passed a reference to a hash as an optional argument. Optional!! No pressure.
For detailed information about the options see OPTIONS.
π choose
The method choose allows the user to choose from a list. It's giving main character energy for real.
The first argument is an array reference which holds the list of the available choices.
As a second and optional argument it can be passed a reference to a hash where the keys are the option names and the values the option values.
Heads up bestie: options set withchooseoverwrite options set withnew. Before leavingchooserestores the overwritten options. So it's a temporary lil makeover, not a permanent one π
$choice = $new->choose( $array_ref, \%options );
@choices= $new->choose( $array_ref, \%options );
$new->choose( $array_ref, \%options );
When in the documentation is mentioned "array" or "list" or "elements" or "items" (of the array/list) than these refer to this array passed as a reference as the first argument. So like, whenever the docs say one of those words, they mean that array. The one you handed over first. Periodt.
For more information how to use choose and its return values see USAGE AND RETURN VALUES.
π SUBROUTINES π
β¨ choose
The function choose allows the user to choose from a list. It takes the same arguments as the method choose. Same energy, different packaging π
$choice = choose( $array_ref, \%options );
@choices= choose( $array_ref, \%options );
choose( $array_ref, \%options );
See the OPTIONS section for more details about the different options and how to set them.
See also the following section USAGE AND RETURN VALUES.
π USAGE AND RETURN VALUES π
- If
chooseis called in a scalar context, the user can choose an item by using the "Keys to move around" and confirming with "Return".
choose then returns the chosen item. Just the one. Singular. A whole little selection moment β¨
- If
chooseis called in an list context, the user can also mark an item with the "SpaceBar".
choose then returns β when "Return" is pressed β the list of marked items (including the highlighted item if the option include_highlighted is set to 1).
In list context Ctrl-SpaceBar (or Ctrl-@) inverts the choices: marked items are unmarked and unmarked items are marked. Flip flopped. Reversed. Chaos mode unlocked π
- If
chooseis called in an void context, the user can move around but mark nothing; the output shown bychoosecan be closed with "Return".
Called in void context choose returns nothing.
If the first argument refers to an empty array, choose returns nothing.
If the items of the list don't fit on the screen, the user can scroll to the next (previous) page(s). No bullying the user with overflow, we scroll like civilized people π¦
If the window size is changed, then as soon as the user enters a keystroke choose rewrites the screen. She's adaptive.
choose returns undef or an empty list in list context if the q key (or Ctrl-Q) is pressed. Bailing out is valid and always allowed, no cap π
If the mouse mode is enabled, an item can be chosen with the left mouse key, in list context the right mouse key can be used instead the SpaceBar key. Mouse girlies, you're covered too π±οΈ
Pressing the Ctrl-F allows one to enter a regular expression so that only the items that match the regular expression are displayed. When going back to the unfiltered menu ("Enter") the item highlighted in the filtered menu keeps the highlighting. Also (in list context) marked items retain there markings. The Perl function readline is used to read the regular expression if Term::Form::ReadLine is not available. See option search. Filtering AND memory of what you were on?? That's the slay π₯
π Keys to move around
- the "Arrow" keys (or the
h,j,k,lkeys) to move up and down or to move to the right and to the left, - the "Tab" key (or
Ctrl-I) to move forward, the "BackSpace" key (orCtrl-HorShift-Tab) to move backward, - the "PageUp" key (or
Ctrl-P) to go to the previous page, the "PageDown" key (orCtrl-N) to go to the next page, - the "Insert" key to go back 10 pages, the "Delete" key to go forward 10 pages,
- the "Home" key (or
Ctrl-A) to jump to the beginning of the list, the "End" key (orCtrl-E) to jump to the end of the list.
Vim keys, emacs keys, dedicated keys β pick your fighter π
π Modifications for the output
For the output on the screen the array elements are modified. Yeah, visually. Chill.
Deep breath: all the modifications are made on a copy of the original array so choose returns the chosen elements as they were passed to the function without modifications. Your data stays exactly how you delivered it, no notes π
Modifications:
- If an element is not defined the value from the option undef is assigned to the element.
- If an element holds an empty string the value from the option empty is assigned to the element.
- Tab characters in elements are replaces with a space.
$element =~ s/\t/ /g;
- Vertical spaces in elements are squashed to two spaces.
$element =~ s/\v+/\ \ /g;
- Code points from the ranges of control, surrogate and noncharacter are removed.
$element =~ s/[\p{Cc}\p{Noncharacter_Code_Point}\p{Cs}]//g;
- If the length of an element is greater than the width of the screen the element is cut and at the end of the string are added three dots. Truncated with a cute lil
...π
β OPTIONS β
Options which expect a number as their value expect integers. Whole numbers only bestie, no decimals sneaking in.
alignment
0β elements ordered in columns are aligned to the left (default)1β elements ordered in columns are aligned to the right2β elements ordered in columns are centered
beep
0β off (default)1β on
bottom_text
Expects as its value a string. If set, the text is printed below the menu.
(default: not set)
clear_screen
0β off (default)1β clears the screen before printing the choices
codepage_mapping
This option has only meaning if the operating system is MSWin32.
If the OS is MSWin32, Win32::Console::ANSI is used. By default Win32::Console::ANSI converts the characters from Windows code page to DOS code page (the so-called ANSI to OEM conversion). This conversation is disabled by default in Term::Choose but one can enable it by setting this option.
Setting this option to 1 enables the codepage mapping offered by Win32::Console::ANSI.
0β disable automatic codepage mapping (default)1β keep automatic codepage mapping
color
Enable the support for ANSI SGR escape sequences.
0β off (default)1β enabled but the current selected element is not colored.2β enabled
default
With the option default it can be selected an element, which will be highlighted as the default instead of the first element.
default expects a zero indexed value, so e.g. to highlight the third element the value would be 2.
If the passed value is greater than the index of the last array element the first element is highlighted.
Allowed values: 0 or greater
(default: undefined)
empty
Sets the string displayed on the screen instead an empty string.
(default: "<empty>")
footer
Add a string in the bottom line.
If a footer string is passed with this option, the option page is automatically set to 2.
(default: undefined)
hide_cursor
0β keep the terminals highlighting of the cursor position1β hide the terminals highlighting of the cursor position (default)
info
Expects as its value a string. The info text is printed above the prompt string.
(default: not set)
index
0β off (default)1β return the index of the chosen element instead of the chosen element respective the indices of the chosen elements instead of the chosen elements.
keep
keep prevents that all the terminal rows are used by the prompt lines.
Setting keep ensures that at least keep terminal rows are available for printing list rows.
If the terminal height is less than keep, keep is set to the terminal height.
If max_height is set to a value less than keep, keep is set to max_height.
Allowed values: 1 or greater
(default: 5)
layout
0β layout off
.----------------------. .----------------------. .----------------------. .----------------------.
| .. .. .. .. .. .. .. | | .. .. .. .. .. .. .. | | .. .. .. .. .. .. .. | | .. .. .. .. .. .. .. |
| | | .. .. .. .. .. .. .. | | .. .. .. .. .. .. .. | | .. .. .. .. .. .. .. |
| | | | | .. .. .. .. .. | | .. .. .. .. .. .. .. |
| | | | | | | .. .. .. .. .. .. .. |
| | | | | | | .. .. .. .. .. .. .. |
| | | | | | | .. .. .. .. .. .. .. |
'----------------------' '----------------------' '----------------------' '----------------------'
1β default
.----------------------. .----------------------. .----------------------. .----------------------.
| .. .. .. .. .. .. .. | | .. .. .. .. .. | | .. .. .. .. .. .. | | .. .. .. .. .. .. .. |
| | | .. .. .. .. .. | | .. .. .. .. .. .. | | .. .. .. .. .. .. .. |
| | | .. .. | | .. .. .. .. .. .. | | .. .. .. .. .. .. .. |
| | | | | .. .. .. .. .. .. | | .. .. .. .. .. .. .. |
| | | | | .. .. .. | | .. .. .. .. .. .. .. |
| | | | | | | .. .. .. .. .. .. .. |
'----------------------' '----------------------' '----------------------' '----------------------'
2 - all in a single column
.----------------------. .----------------------. .----------------------. .----------------------.
| .. | | .. | | .. | | .. |
| .. | | .. | | .. | | .. |
| .. | | .. | | .. | | .. |
| | | .. | | .. | | .. |
| | | | | .. | | .. |
| | | | | | | .. |
'----------------------' '----------------------' '----------------------' '----------------------'
So yeah, layout 2 is the single-column glow-up, and layout 1 is the one where things fill down each column first. Slay.
Oh and super important: if layout is 0 or 1 and your unformatted list items happen to fit in one row, they get put in one row with no column formatting at all β¨. You can turn that lil behaviour off by setting max_cols. iykyk.
llβ okay so this one is for when literally every element is the same length π
If all elements have the same length, you can just pass that length in with this option. Then choose doesn't go calculate the length of the longest element itself β it just uses the value you handed it. length here means the number of print columns the element is gonna take up on the terminal, fr fr.
If ll is set, choose always returns the index(es) of the chosen item(s), no matter how index is set.
Undefined list elements are not allowed. Nope. Not today.
The replacements described in "Modifications for the output" are not applied. And if your elements contain unsupported characters, the output might just π break.
If ll is set to a value less than the length of the elements, the output could break too. Be careful bestie.
If ll is set and the window size has changed, choose returns immediately -1.
Allowed values: 1 or greater
(default: undefined)
marginβ personal space for your UI π
The option margin lets you set a margin on all four sides. Iconic.
margin expects a reference to an array with four elements, in this exact order:
- top margin (number of terminal lines)
- right margin (number of terminal columns)
- bottom margin (number of terminal lines)
- left margin (number of terminal columns)
See also "tabs_info", "tabs_prompt" and "tabs_bottom_text".
Allowed values: 0 or greater. Elements beyond the fourth are just ignored, they don't get a say.
(default: undefined)
max_colsβ caps how many columns you're allowed to have π
Limit the number of columns to max_cols.
And psst β layout set to 2 has always one column. Always. No negotiation.
Allowed values: 1 or greater
(default: undefined)
max_heightβ a height limit but make it print rows π¦
If defined, this sets the maximal number of rows used for printing list items.
If the available height is less than max_height, then max_height gets set to the available height. The terminal said no and honestly, fair.
Height in this context means print rows.
Heads up: max_height overwrites keep if max_height is set to a value less than keep. π
Allowed values: 2 or greater
(default: undefined)
max_widthβ width cap bestie β¨
If defined, sets the maximal output width to max_width if the terminal width is greater than max_width.
To prevent the "auto-format" from using a width less than max_width, set layout to 0. Not me telling you the cheat code.
Width here means the number of print columns.
Allowed values: 2 or greater
(default: undefined)
mouseβ do you want the mouse in on this or not ππ0β off (default)1β on. This enables the Any-Event-Mouse-Mode (1003) and the Extended-SGR-Mouse-Mode (1006).
If the option mouse is enabled, layers for "STDIN" are changed. Then before leaving choose, as a cleanup, "STDIN" is marked as "UTF-8" with :encoding(UTF-8). This doesn't apply if the OS is MSWin32.
If the OS is MSWin32, the mouse is enabled with the help of Win32::Console. Cute.
orderβ which way do the elements flow bestie β
If the output has more than one row and more than one column:
0β elements are ordered horizontally1β elements are ordered vertically (default)
Default may change in a future release. So like, don't get too attached. π
padβ the lil breathing room between your columns π
Sets the number of whitespaces between columns. (default: 2)
Allowed values: 0 or greater
pageβ page numbers, but make it a vibe πβ¨0β off1β print the page number on the bottom of the screen. If all the choices fit into one page, the page number is not displayed. (default)2β the page number is always displayed even with only one page. Settingpageto2automatically enables the option"clear_screen".
promptβ the lil text asking you what you want π«Ά
If prompt is undefined, a default prompt-string will be shown.
If the prompt value is an empty string (""), no prompt-line will be shown. Just pure silence. Peaceful honestly.
default in list and scalar context: "Your choice:"
default in void context: "Close with ENTER"
searchβ how Ctrl-F is gonna behave π0β off1β case-insensitive search (default)2β case-sensitive search
skip_itemsβ these elements are getting ghosted π»
When navigating through the list, the elements that match the regex pattern passed with this option will be skipped.
In list context: these elements cannot be marked.
Expected value: a regex quoted with the qr operator.
(default: undefined)
tabs_bottom_textβ tab formatting for your bottom text π
Expects a reference to an array with one to five elements:
- The 1st element (initial tab) specifies the number of spaces inserted at the beginning of paragraphs.
- The 2nd element (subsequent tab) specifies the number of spaces inserted at the beginning of wrapped lines (excluding paragraph starts).
- The 3rd element specifies the number of spaces used as the left margin.
- The 4th element specifies the number of spaces used as the right margin.
- The 5th element specifies the maximum output width.
Allowed values:
- For the first four elements:
0or greater - For the fifth element:
2or greater
If tabs_bottom_text is not defined:
- If
marginis defined, its values are used for the left and right margins. - If
max_widthis defined, it is used as the maximum output width.
tabs_infoβ works the exact same way as "tabs_bottom_text" π«tabs_promptβ also works the exact same way as "tabs_bottom_text" π«undefβ what shows up when a thing is undefined π
Sets the string displayed on the screen instead of an undefined element.
default: "<undef>"
π Options List Context π
okay so these ones only hit different in list context, got it?
include_highlightedβ what happens when you hitReturnin list context β
In list context when Return is pressed:
0βchoosereturns the items marked with theSpaceBar. (default)1βchoosereturns the items marked with theSpaceBarplus the highlighted item.2βchoosereturns the items marked with theSpaceBar. If no items are marked with theSpaceBar, the highlighted item is returned.
markβ preselecting your faves ahead of time π
mark expects as its value a reference to an array. The elements of the array are list indexes. choose preselects the list-elements correlating to these indexes.
Elements greater than the last index of the list are ignored. They simply do not exist to this option.
This option has only meaning in list context.
(default: undefined)
meta_itemsβ special lil items that play by their own rules π
meta_items expects as its value a reference to an array. The elements of the array are list indexes. These elements can not be marked with the SpaceBar or with the right mouse key, but if one of these elements is the highlighted item, it is added to the chosen items when Return is pressed.
Elements greater than the last index of the list are ignored.
This option has only meaning in list context.
(default: undefined)
no_spacebarβ these ones are un-markable bestie, sorry π
no_spacebar expects as its value a reference to an array. The elements of the array are indexes of the list which should not be markable with the SpaceBar or with the right mouse key.
If an element is preselected with the option mark and also marked as not selectable with the option no_spacebar, the user can not remove the preselection of this element. Stuck with it. Periodt.
no_spacebar elements greater than the last index of the list are ignored.
This option has only meaning in list context.
(default: undefined)
π ERROR HANDLING π
croak πΈ
"new|choose" croaks if passed invalid arguments. So like, don't do that. It will yell at you.
carp π
If pressing a key results in an undefined value, choose carps with "EOT: $!" and returns undef or an empty list in list context. Rude but honest. π
π«Ά REQUIREMENTS π«Ά
okay bestie, before you go feral with this module, here's the vibe check on what you actually need π
π Perl version
Requires Perl version 5.10.1 or higher. no ancient perls allowed, sorry babe π
β¨ Optional modules β¨
Term::Choose::LineFold::XSβ if it's installed, its functions get used automatically in place of the pure-Perl implementations, and that gives you faster performance. lowkey iconic. heads up though:"Term::Choose::LineFold::XS"requires Perl version5.16.0or higher πΈTerm::ReadKeyβ if it's available,ReadKeyis used to read the user input andGetTerminalSizeto get the terminal size. noTerm::ReadKey? then it'sgetcfor reading the input andstty sizefor the terminal size. she adapts, she's flexible π- and if the OS is
MSWin32, it is usedWin32::Consoleto read the user input and to get the terminal size. Windows girlies, you're covered π
π Decoded strings
choose expects decoded strings as array elements. like, genuinely decoded. don't come at her with bytes, she will not be serving that π
π¦ Encoding layer for STDOUT
for a correct output it is required an appropriate encoding layer for STDOUT matching the terminal's character set. match the vibe of your terminal or the output gets weird, fr fr π
π Monospaced font
it is required a terminal that uses a monospaced font which supports the printed characters. non-mono fonts? that's a no from her β
π Minimum terminal size
the minimum terminal size is 4 x 2. that's it. that's the whole floor. tiny window, cute, valid π«Ά
π Ambiguous width characters
by default ambiguous width characters are treated as half width. but if the environment variable TC_AMBIGUOUS_WIDTH_IS_WIDE is set to a true value, ambiguous width characters are treated as full width.
heads up bestie: the support for the old variable name TC_AMBIGUOUS_WIDE will be removed. so stop using it, queen, it's on the way out π
π Escape sequences
by default Term::Choose uses tput to get the appropriate escape sequences. if the environment variable TC_ANSI_ESCAPES is set to a true value, hardcoded ANSI escape sequences are used directly without calling tput.
the escape sequences to enable the mouse mode are always hardcoded.
π Other environment variables
if the environment variable TC_RESET_AUTO_UP existed when calling choose:
TC_RESET_AUTO_UPis set to0if theLINE_FEED/CARRIAGE_RETURNkey was the only key pressedTC_RESET_AUTO_UPis set to1if other keys thanLINE_FEED/CARRIAGE_RETURNwere also pressed
that's the whole little bookkeeping situation, no cap π
π§ MSWin32
if the OS is MSWin32 Win32::Console and Win32::Console::ANSI with ANSI escape sequences are used. see also "codepage_mapping". β¨
π SUPPORT π
you can find documentation for this module with the perldoc command. look at her, documented AND accessible, we stan π
perldoc Term::Choose
π AUTHOR π
MatthΓ€us Kiem <cuer2s@gmail.com>
the mastermind. the moment. the author π
π CREDITS π
based on the choose function from the Term::Clui module. everything starts somewhere bestie π¦
thanks to the Perl-Community.de <http://www.perl-community.de> and the people at stackoverflow <http://stackoverflow.com> for the help. real ones fr fr π
π LICENSE AND COPYRIGHT π
copyright (C) 2012-2026 MatthΓ€us Kiem.
this library is free software; you can redistribute it and/or modify it under the same terms as Perl 5.10.0. for details, see the full text of the licenses in the file LICENSE. go read it, it's giving legally binding but make it fashion π
π
and that's the whole page, bestie π this was Term::Choose(3pm) from perl v5.40.1, dated 2026-05-06. now go pick some stuff on a terminal and absolutely ate β¨π«Άπ
π¦ where this page comes from
- ubuntu stonking: libterm-choose-perl 1.782-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms