CINXE.COM

Term::UI - Term::ReadLine UI made easy - Perldoc Browser

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Term::UI - Term::ReadLine UI made easy - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/Term::UI"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" /> <style> body { background: #f4f4f5; color: #020202; } .navbar-dark { background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%); background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%); background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0); background-repeat: repeat-x; } .navbar-dark .navbar-nav .nav-link, .navbar-dark .navbar-nav .nav-link:focus { color: #fff } .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 } #wrapperlicious { margin: 0 auto; font: 0.9em 'Helvetica Neue', Helvetica, sans-serif; font-weight: normal; line-height: 1.5em; margin: 0; padding: 0; } #wrapperlicious h1 { font-size: 1.5em } #wrapperlicious h2 { font-size: 1.3em } #wrapperlicious h3 { font-size: 1.1em } #wrapperlicious h4 { font-size: 0.9em } #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3, #wrapperlicious h4, #wrapperlicious dt { color: #020202; margin-top: 1em; margin-bottom: 1em; position: relative; font-weight: bold; } #wrapperlicious a { color: inherit; text-decoration: underline } #wrapperlicious #toc { text-decoration: none } #wrapperlicious a:hover { color: #2a2a2a } #wrapperlicious a img { border: 0 } #wrapperlicious :not(pre) > code { color: inherit; background-color: rgba(0, 0, 0, 0.04); border-radius: 3px; font: 0.9em Consolas, Menlo, Monaco, monospace; padding: 0.3em; } #wrapperlicious dd { margin: 0; margin-left: 2em; } #wrapperlicious dt { color: #2a2a2a; font-weight: bold; margin-left: 0.9em; } #wrapperlicious p { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious li > p { margin-bottom: 0; margin-top: 0; } #wrapperlicious pre { border: 1px solid #c1c1c1; border-radius: 3px; font: 100% Consolas, Menlo, Monaco, monospace; margin-bottom: 1em; margin-top: 1em; } #wrapperlicious pre > code { display: block; background-color: #f6f6f6; font: 0.9em Consolas, Menlo, Monaco, monospace; line-height: 1.5em; text-align: left; white-space: pre; padding: 1em; } #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious ul { list-style-type: square; } #wrapperlicious ul ul { margin-bottom: 0px; margin-top: 0px; } #footer { font-size: 0.8em; padding-top: 0.5em; text-align: center; } #more { display: inline; font-size: 0.8em; } #perldocdiv { background-color: #fff; border: 1px solid #c1c1c1; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; margin-left: auto; margin-right: auto; padding: 3em; padding-top: 1em; max-width: 960px; } #moduleversion { float: right } #wrapperlicious .leading-notice { font-style: italic; padding-left: 1em; margin-top: 1em; margin-bottom: 1em; } #wrapperlicious .permalink { display: none; left: -0.75em; position: absolute; padding-right: 0.25em; text-decoration: none; } #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink, #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink, #wrapperlicious dt:hover .permalink { display: block; } </style> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-KVNWBNT5FB'); gtag('config', 'UA-50555-3'); </script> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="/"><img src="/images/perl_camel_30.png" width="30" height="30" class="d-inline-block align-top" alt="Perl Camel Logo"> Perldoc Browser</a> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav mr-auto"> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.18.3</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/Term::UI">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/Term::UI">5.40.1</a> <a class="dropdown-item" href="/5.40.0/Term::UI">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/Term::UI">5.38.3</a> <a class="dropdown-item" href="/5.38.2/Term::UI">5.38.2</a> <a class="dropdown-item" href="/5.38.1/Term::UI">5.38.1</a> <a class="dropdown-item" href="/5.38.0/Term::UI">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/Term::UI">5.36.3</a> <a class="dropdown-item" href="/5.36.2/Term::UI">5.36.2</a> <a class="dropdown-item" href="/5.36.1/Term::UI">5.36.1</a> <a class="dropdown-item" href="/5.36.0/Term::UI">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/Term::UI">5.34.3</a> <a class="dropdown-item" href="/5.34.2/Term::UI">5.34.2</a> <a class="dropdown-item" href="/5.34.1/Term::UI">5.34.1</a> <a class="dropdown-item" href="/5.34.0/Term::UI">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/Term::UI">5.32.1</a> <a class="dropdown-item" href="/5.32.0/Term::UI">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/Term::UI">5.30.3</a> <a class="dropdown-item" href="/5.30.2/Term::UI">5.30.2</a> <a class="dropdown-item" href="/5.30.1/Term::UI">5.30.1</a> <a class="dropdown-item" href="/5.30.0/Term::UI">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/Term::UI">5.28.3</a> <a class="dropdown-item" href="/5.28.2/Term::UI">5.28.2</a> <a class="dropdown-item" href="/5.28.1/Term::UI">5.28.1</a> <a class="dropdown-item" href="/5.28.0/Term::UI">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/Term::UI">5.26.3</a> <a class="dropdown-item" href="/5.26.2/Term::UI">5.26.2</a> <a class="dropdown-item" href="/5.26.1/Term::UI">5.26.1</a> <a class="dropdown-item" href="/5.26.0/Term::UI">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/Term::UI">5.24.4</a> <a class="dropdown-item" href="/5.24.3/Term::UI">5.24.3</a> <a class="dropdown-item" href="/5.24.2/Term::UI">5.24.2</a> <a class="dropdown-item" href="/5.24.1/Term::UI">5.24.1</a> <a class="dropdown-item" href="/5.24.0/Term::UI">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/Term::UI">5.22.4</a> <a class="dropdown-item" href="/5.22.3/Term::UI">5.22.3</a> <a class="dropdown-item" href="/5.22.2/Term::UI">5.22.2</a> <a class="dropdown-item" href="/5.22.1/Term::UI">5.22.1</a> <a class="dropdown-item" href="/5.22.0/Term::UI">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/Term::UI">5.20.3</a> <a class="dropdown-item" href="/5.20.2/Term::UI">5.20.2</a> <a class="dropdown-item" href="/5.20.1/Term::UI">5.20.1</a> <a class="dropdown-item" href="/5.20.0/Term::UI">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/Term::UI">5.18.4</a> <a class="dropdown-item active" href="/5.18.3/Term::UI">5.18.3</a> <a class="dropdown-item" href="/5.18.2/Term::UI">5.18.2</a> <a class="dropdown-item" href="/5.18.1/Term::UI">5.18.1</a> <a class="dropdown-item" href="/5.18.0/Term::UI">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/Term::UI">5.16.3</a> <a class="dropdown-item" href="/5.16.2/Term::UI">5.16.2</a> <a class="dropdown-item" href="/5.16.1/Term::UI">5.16.1</a> <a class="dropdown-item" href="/5.16.0/Term::UI">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/Term::UI">5.14.4</a> <a class="dropdown-item" href="/5.14.3/Term::UI">5.14.3</a> <a class="dropdown-item" href="/5.14.2/Term::UI">5.14.2</a> <a class="dropdown-item" href="/5.14.1/Term::UI">5.14.1</a> <a class="dropdown-item" href="/5.14.0/Term::UI">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/Term::UI">5.12.5</a> <a class="dropdown-item" href="/5.12.4/Term::UI">5.12.4</a> <a class="dropdown-item" href="/5.12.3/Term::UI">5.12.3</a> <a class="dropdown-item" href="/5.12.2/Term::UI">5.12.2</a> <a class="dropdown-item" href="/5.12.1/Term::UI">5.12.1</a> <a class="dropdown-item" href="/5.12.0/Term::UI">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/Term::UI">5.10.1</a> <a class="dropdown-item" href="/5.10.0/Term::UI">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/Term::UI">5.8.9</a> <a class="dropdown-item" href="/5.8.8/Term::UI">5.8.8</a> <a class="dropdown-item" href="/5.8.7/Term::UI">5.8.7</a> <a class="dropdown-item" href="/5.8.6/Term::UI">5.8.6</a> <a class="dropdown-item" href="/5.8.5/Term::UI">5.8.5</a> <a class="dropdown-item" href="/5.8.4/Term::UI">5.8.4</a> <a class="dropdown-item" href="/5.8.3/Term::UI">5.8.3</a> <a class="dropdown-item" href="/5.8.2/Term::UI">5.8.2</a> <a class="dropdown-item" href="/5.8.1/Term::UI">5.8.1</a> <a class="dropdown-item" href="/5.8.0/Term::UI">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/Term::UI">5.6.2</a> <a class="dropdown-item" href="/5.6.1/Term::UI">5.6.1</a> <a class="dropdown-item" href="/5.6.0/Term::UI">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/Term::UI">5.005_04</a> <a class="dropdown-item" href="/5.005_03/Term::UI">5.005_03</a> <a class="dropdown-item" href="/5.005_02/Term::UI">5.005_02</a> <a class="dropdown-item" href="/5.005_01/Term::UI">5.005_01</a> <a class="dropdown-item" href="/5.005/Term::UI">5.005</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-dev"> <a class="dropdown-item" href="/blead/Term::UI">blead</a> <a class="dropdown-item" href="/5.41.10/Term::UI">5.41.10</a> <a class="dropdown-item" href="/5.41.9/Term::UI">5.41.9</a> <a class="dropdown-item" href="/5.41.8/Term::UI">5.41.8</a> <a class="dropdown-item" href="/5.41.7/Term::UI">5.41.7</a> <a class="dropdown-item" href="/5.41.6/Term::UI">5.41.6</a> <a class="dropdown-item" href="/5.41.5/Term::UI">5.41.5</a> <a class="dropdown-item" href="/5.41.4/Term::UI">5.41.4</a> <a class="dropdown-item" href="/5.41.3/Term::UI">5.41.3</a> <a class="dropdown-item" href="/5.41.2/Term::UI">5.41.2</a> <a class="dropdown-item" href="/5.41.1/Term::UI">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/Term::UI">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/Term::UI">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/Term::UI">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/Term::UI">5.39.10</a> <a class="dropdown-item" href="/5.39.9/Term::UI">5.39.9</a> <a class="dropdown-item" href="/5.39.8/Term::UI">5.39.8</a> <a class="dropdown-item" href="/5.39.7/Term::UI">5.39.7</a> <a class="dropdown-item" href="/5.39.6/Term::UI">5.39.6</a> <a class="dropdown-item" href="/5.39.5/Term::UI">5.39.5</a> <a class="dropdown-item" href="/5.39.4/Term::UI">5.39.4</a> <a class="dropdown-item" href="/5.39.3/Term::UI">5.39.3</a> <a class="dropdown-item" href="/5.39.2/Term::UI">5.39.2</a> <a class="dropdown-item" href="/5.39.1/Term::UI">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/Term::UI">5.38.3-RC1</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-nav"> <a class="dropdown-item" href="/5.18.3/perl">Perl</a> <a class="dropdown-item" href="/5.18.3/perlintro">Intro</a> <a class="dropdown-item" href="/5.18.3/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.18.3/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.18.3/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.3/perlop">Operators</a> <a class="dropdown-item" href="/5.18.3/functions">Functions</a> <a class="dropdown-item" href="/5.18.3/variables">Variables</a> <a class="dropdown-item" href="/5.18.3/modules">Modules</a> <a class="dropdown-item" href="/5.18.3/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.3/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.18.3/perlcommunity">Community</a> <a class="dropdown-item" href="/5.18.3/perlhist">History</a> </div> </li> </ul> <ul class="navbar-nav"> <script> function set_expand (expand) { var perldocdiv = document.getElementById('perldocdiv'); var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width'); var expanded = (width == '' || width == 'none') ? true : false; if (expand === null) { expand = !expanded; } if ((expand && !expanded) || (!expand && expanded)) { perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px'); var button_classlist = document.getElementById('content-expand-button').classList; if (expand) { button_classlist.add('btn-light'); button_classlist.remove('btn-outline-light'); } else { button_classlist.add('btn-outline-light'); button_classlist.remove('btn-light'); } } return expand; } function toggle_expand () { var expand = set_expand(null); document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC'; } function read_expand () { return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { if (read_expand()) { set_expand(true); } }); } else if (read_expand()) { set_expand(true); } </script> <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-4" onclick="toggle_expand()">Expand</button> </ul> <form class="form-inline" method="get" action="/5.18.3/search"> <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search" value=""> </form> </div> </nav> <div id="wrapperlicious" class="container-fluid"> <div id="perldocdiv"> <div id="links"> <a href="/5.18.3/Term::UI">Term::UI</a> <div id="more"> (<a href="/5.18.3/Term::UI.txt">source</a>, <a href="https://metacpan.org/pod/Term::UI">CPAN</a>) </div> <div id="moduleversion">version 0.34</div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.18.3. <a href="/Term::UI">View the latest version</a> </div> <h1><a id="toc">CONTENTS</a></h1> <ul> <li> <a class="text-decoration-none" href="#NAME">NAME</a> </li> <li> <a class="text-decoration-none" href="#SYNOPSIS">SYNOPSIS</a> </li> <li> <a class="text-decoration-none" href="#DESCRIPTION">DESCRIPTION</a> </li> <li> <a class="text-decoration-none" href="#HOW-IT-WORKS">HOW IT WORKS</a> </li> <li> <a class="text-decoration-none" href="#METHODS">METHODS</a> <ul> <li> <a class="text-decoration-none" href="#$reply-=-$term-%3Eget_reply(-prompt-=%3E-&#39;question?&#39;,-%5Bchoices-=%3E-%5C@list,-default-=%3E-$list%5B0%5D,-multi-=%3E-BOOL,-print_me-=%3E-%22extra-text-to-print-&amp;-record%22,-allow-=%3E-$ref%5D-);">$reply = $term-&gt;get_reply( prompt =&gt; &#39;question?&#39;, [choices =&gt; \@list, default =&gt; $list[0], multi =&gt; BOOL, print_me =&gt; &quot;extra text to print &amp; record&quot;, allow =&gt; $ref] );</a> </li> <li> <a class="text-decoration-none" href="#$bool-=-$term-%3Eask_yn(-prompt-=%3E-%22your-question%22,-%5Bdefault-=%3E-(y%7C1,n%7C0),-print_me-=%3E-%22extra-text-to-print-&amp;-record%22%5D-)">$bool = $term-&gt;ask_yn( prompt =&gt; &quot;your question&quot;, [default =&gt; (y|1,n|0), print_me =&gt; &quot;extra text to print &amp; record&quot;] )</a> </li> <li> <a class="text-decoration-none" href="#($opts,-$munged)-=-$term-%3Eparse_options(-STRING-);">($opts, $munged) = $term-&gt;parse_options( STRING );</a> </li> <li> <a class="text-decoration-none" href="#$str-=-$term-%3Ehistory_as_string">$str = $term-&gt;history_as_string</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#GLOBAL-VARIABLES">GLOBAL VARIABLES</a> <ul> <li> <a class="text-decoration-none" href="#$Term::UI::VERBOSE">$Term::UI::VERBOSE</a> </li> <li> <a class="text-decoration-none" href="#$Term::UI::AUTOREPLY">$Term::UI::AUTOREPLY</a> </li> <li> <a class="text-decoration-none" href="#$Term::UI::INVALID">$Term::UI::INVALID</a> </li> <li> <a class="text-decoration-none" href="#$Term::UI::History::HISTORY_FH">$Term::UI::History::HISTORY_FH</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#EXAMPLES">EXAMPLES</a> <ul> <li> <a class="text-decoration-none" href="#Basic-get_reply-sample">Basic get_reply sample</a> </li> <li> <a class="text-decoration-none" href="#get_reply-with-choices">get_reply with choices</a> </li> <li> <a class="text-decoration-none" href="#get_reply-with-choices-and-default">get_reply with choices and default</a> </li> <li> <a class="text-decoration-none" href="#get_reply-using-print_me-&amp;-multi">get_reply using print_me &amp; multi</a> </li> <li> <a class="text-decoration-none" href="#get_reply-&amp;-allow">get_reply &amp; allow</a> </li> <li> <a class="text-decoration-none" href="#an-elaborate-ask_yn-sample">an elaborate ask_yn sample</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#See-Also">See Also</a> </li> <li> <a class="text-decoration-none" href="#BUG-REPORTS">BUG REPORTS</a> </li> <li> <a class="text-decoration-none" href="#AUTHOR">AUTHOR</a> </li> <li> <a class="text-decoration-none" href="#COPYRIGHT">COPYRIGHT</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>Term::UI - Term::ReadLine UI made easy</p> <h1 id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">#</a>SYNOPSIS</h1> <pre><code>use Term::UI; use Term::ReadLine; my $term = Term::ReadLine-&gt;new(&#39;brand&#39;); my $reply = $term-&gt;get_reply( prompt =&gt; &#39;What is your favourite colour?&#39;, choices =&gt; [qw|blue red green|], default =&gt; &#39;blue&#39;, ); my $bool = $term-&gt;ask_yn( prompt =&gt; &#39;Do you like cookies?&#39;, default =&gt; &#39;y&#39;, ); my $string = q[some_command -option --no-foo --quux=&#39;this thing&#39;]; my ($options,$munged_input) = $term-&gt;parse_options($string); ### don&#39;t have Term::UI issue warnings -- default is &#39;1&#39; $Term::UI::VERBOSE = 0; ### always pick the default (good for non-interactive terms) ### -- default is &#39;0&#39; $Term::UI::AUTOREPLY = 1; ### Retrieve the entire session as a printable string: $hist = Term::UI::History-&gt;history_as_string; $hist = $term-&gt;history_as_string;</code></pre> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <p><code>Term::UI</code> is a transparent way of eliminating the overhead of having to format a question and then validate the reply, informing the user if the answer was not proper and re-issuing the question.</p> <p>Simply give it the question you want to ask, optionally with choices the user can pick from and a default and <code>Term::UI</code> will DWYM.</p> <p>For asking a yes or no question, there&#39;s even a shortcut.</p> <h1 id="HOW-IT-WORKS"><a class="permalink" href="#HOW-IT-WORKS">#</a><a id="HOW"></a>HOW IT WORKS</h1> <p><code>Term::UI</code> places itself at the back of the <code>Term::ReadLine</code> <code>@ISA</code> array, so you can call its functions through your term object.</p> <p><code>Term::UI</code> uses <code>Term::UI::History</code> to record all interactions with the commandline. You can retrieve this history, or alter the filehandle the interaction is printed to. See the <code>Term::UI::History</code> manpage or the <code>SYNOPSIS</code> for details.</p> <h1 id="METHODS"><a class="permalink" href="#METHODS">#</a>METHODS</h1> <h2 id="$reply-=-$term-&gt;get_reply(-prompt-=&gt;-&#39;question?&#39;,-[choices-=&gt;-\@list,-default-=&gt;-$list[0],-multi-=&gt;-BOOL,-print_me-=&gt;-&quot;extra-text-to-print-&amp;-record&quot;,-allow-=&gt;-$ref]-);"><a class="permalink" href="#$reply-=-$term-%3Eget_reply(-prompt-=%3E-&#39;question?&#39;,-%5Bchoices-=%3E-%5C@list,-default-=%3E-$list%5B0%5D,-multi-=%3E-BOOL,-print_me-=%3E-%22extra-text-to-print-&amp;-record%22,-allow-=%3E-$ref%5D-);">#</a><a id="reply-term-get_reply-prompt-question-choices-list-default-list-0-multi-BOOL-print_me-extra-text-to-print-record-allow-ref"></a>$reply = $term-&gt;get_reply( prompt =&gt; &#39;question?&#39;, [choices =&gt; \@list, default =&gt; $list[0], multi =&gt; BOOL, print_me =&gt; &quot;extra text to print &amp; record&quot;, allow =&gt; $ref] );</h2> <p><code>get_reply</code> asks a user a question, and then returns the reply to the caller. If the answer is invalid (more on that below), the question will be reposed, until a satisfactory answer has been entered.</p> <p>You have the option of providing a list of choices the user can pick from using the <code>choices</code> argument. If the answer is not in the list of choices presented, the question will be reposed.</p> <p>If you provide a <code>default</code> answer, this will be returned when either <code>$AUTOREPLY</code> is set to true, (see the <code>GLOBAL VARIABLES</code> section further below), or when the user just hits <code>enter</code>.</p> <p>You can indicate that the user is allowed to enter multiple answers by toggling the <code>multi</code> flag. Note that a list of answers will then be returned to you, rather than a simple string.</p> <p>By specifying an <code>allow</code> hander, you can yourself validate the answer a user gives. This can be any of the types that the Params::Check <code>allow</code> function allows, so please refer to that manpage for details.</p> <p>Finally, you have the option of adding a <code>print_me</code> argument, which is simply printed before the prompt. It&#39;s printed to the same file handle as the rest of the questions, so you can use this to keep track of a full session of Q&amp;A with the user, and retrieve it later using the <code>Term::UI-&gt;history_as_string</code> function.</p> <p>See the <code>EXAMPLES</code> section for samples of how to use this function.</p> <h2 id="$bool-=-$term-&gt;ask_yn(-prompt-=&gt;-&quot;your-question&quot;,-[default-=&gt;-(y|1,n|0),-print_me-=&gt;-&quot;extra-text-to-print-&amp;-record&quot;]-)"><a class="permalink" href="#$bool-=-$term-%3Eask_yn(-prompt-=%3E-%22your-question%22,-%5Bdefault-=%3E-(y%7C1,n%7C0),-print_me-=%3E-%22extra-text-to-print-&amp;-record%22%5D-)">#</a><a id="bool-term-ask_yn-prompt-your-question-default-y-1-n-0-print_me-extra-text-to-print-record"></a>$bool = $term-&gt;ask_yn( prompt =&gt; &quot;your question&quot;, [default =&gt; (y|1,n|0), print_me =&gt; &quot;extra text to print &amp; record&quot;] )</h2> <p>Asks a simple <code>yes</code> or <code>no</code> question to the user, returning a boolean indicating <code>true</code> or <code>false</code> to the caller.</p> <p>The <code>default</code> answer will automatically returned, if the user hits <code>enter</code> or if <code>$AUTOREPLY</code> is set to true. See the <code>GLOBAL VARIABLES</code> section further below.</p> <p>Also, you have the option of adding a <code>print_me</code> argument, which is simply printed before the prompt. It&#39;s printed to the same file handle as the rest of the questions, so you can use this to keep track of a full session of Q&amp;A with the user, and retrieve it later using the <code>Term::UI-&gt;history_as_string</code> function.</p> <p>See the <code>EXAMPLES</code> section for samples of how to use this function.</p> <h2 id="($opts,-$munged)-=-$term-&gt;parse_options(-STRING-);"><a class="permalink" href="#($opts,-$munged)-=-$term-%3Eparse_options(-STRING-);">#</a><a id="opts-munged-term-parse_options-STRING"></a>($opts, $munged) = $term-&gt;parse_options( STRING );</h2> <p><code>parse_options</code> will convert all options given from an input string to a hash reference. If called in list context it will also return the part of the input string that it found no options in.</p> <p>Consider this example:</p> <pre><code>my $str = q[command --no-foo --baz --bar=0 --quux=bleh ] . q[--option=&quot;some&#39;thing&quot; -one-dash -single=blah&#39; arg]; my ($options,$munged) = $term-&gt;parse_options($str); ### $options would contain: ### $options = { &#39;foo&#39; =&gt; 0, &#39;bar&#39; =&gt; 0, &#39;one-dash&#39; =&gt; 1, &#39;baz&#39; =&gt; 1, &#39;quux&#39; =&gt; &#39;bleh&#39;, &#39;single&#39; =&gt; &#39;blah\&#39;&#39;, &#39;option&#39; =&gt; &#39;some\&#39;thing&#39; }; ### and this is the munged version of the input string, ### ie what&#39;s left of the input minus the options $munged = &#39;command arg&#39;;</code></pre> <p>As you can see, you can either use a single or a double <code>-</code> to indicate an option. If you prefix an option with <code>no-</code> and do not give it a value, it will be set to 0. If it has no prefix and no value, it will be set to 1. Otherwise, it will be set to its value. Note also that it can deal fine with single/double quoting issues.</p> <h2 id="$str-=-$term-&gt;history_as_string"><a class="permalink" href="#$str-=-$term-%3Ehistory_as_string">#</a><a id="str-term-history_as_string"></a>$str = $term-&gt;history_as_string</h2> <p>Convenience wrapper around <code>Term::UI::History-&gt;history_as_string</code>.</p> <p>Consult the <code>Term::UI::History</code> man page for details.</p> <h1 id="GLOBAL-VARIABLES"><a class="permalink" href="#GLOBAL-VARIABLES">#</a><a id="GLOBAL"></a>GLOBAL VARIABLES</h1> <p>The behaviour of Term::UI can be altered by changing the following global variables:</p> <h2 id="$Term::UI::VERBOSE"><a class="permalink" href="#$Term::UI::VERBOSE">#</a><a id="Term::UI::VERBOSE"></a>$Term::UI::VERBOSE</h2> <p>This controls whether Term::UI will issue warnings and explanations as to why certain things may have failed. If you set it to 0, Term::UI will not output any warnings. The default is 1;</p> <h2 id="$Term::UI::AUTOREPLY"><a class="permalink" href="#$Term::UI::AUTOREPLY">#</a><a id="Term::UI::AUTOREPLY"></a>$Term::UI::AUTOREPLY</h2> <p>This will make every question be answered by the default, and warn if there was no default provided. This is particularly useful if your program is run in non-interactive mode. The default is 0;</p> <h2 id="$Term::UI::INVALID"><a class="permalink" href="#$Term::UI::INVALID">#</a><a id="Term::UI::INVALID"></a>$Term::UI::INVALID</h2> <p>This holds the string that will be printed when the user makes an invalid choice. You can override this string from your program if you, for example, wish to do localization. The default is <code>Invalid selection, please try again: </code></p> <h2 id="$Term::UI::History::HISTORY_FH"><a class="permalink" href="#$Term::UI::History::HISTORY_FH">#</a><a id="Term::UI::History::HISTORY_FH"></a>$Term::UI::History::HISTORY_FH</h2> <p>This is the filehandle all the print statements from this module are being sent to. Please consult the <code>Term::UI::History</code> manpage for details.</p> <p>This defaults to <code>*STDOUT</code>.</p> <h1 id="EXAMPLES"><a class="permalink" href="#EXAMPLES">#</a>EXAMPLES</h1> <h2 id="Basic-get_reply-sample"><a class="permalink" href="#Basic-get_reply-sample">#</a><a id="Basic"></a>Basic get_reply sample</h2> <pre><code>### ask a user (with an open question) for their favourite colour $reply = $term-&gt;get_reply( prompt =&gt; &#39;Your favourite colour? );</code></pre> <p>which would look like:</p> <pre><code class="plaintext">Your favourite colour?</code></pre> <p>and <code>$reply</code> would hold the text the user typed.</p> <h2 id="get_reply-with-choices"><a class="permalink" href="#get_reply-with-choices">#</a><a id="get_reply"></a>get_reply with choices</h2> <pre><code>### now provide a list of choices, so the user has to pick one $reply = $term-&gt;get_reply( prompt =&gt; &#39;Your favourite colour?&#39;, choices =&gt; [qw|red green blue|] );</code></pre> <p>which would look like:</p> <pre><code class="plaintext"> 1&gt; red 2&gt; green 3&gt; blue Your favourite colour?</code></pre> <p><code>$reply</code> will hold one of the choices presented. <code>Term::UI</code> will repose the question if the user attempts to enter an answer that&#39;s not in the list of choices. The string presented is held in the <code>$Term::UI::INVALID</code> variable (see the <code>GLOBAL VARIABLES</code> section for details.</p> <h2 id="get_reply-with-choices-and-default"><a class="permalink" href="#get_reply-with-choices-and-default">#</a><a id="get_reply1"></a>get_reply with choices and default</h2> <pre><code>### provide a sensible default option -- everyone loves blue! $reply = $term-&gt;get_reply( prompt =&gt; &#39;Your favourite colour?&#39;, choices =&gt; [qw|red green blue|], default =&gt; &#39;blue&#39; );</code></pre> <p>which would look like:</p> <pre><code class="plaintext"> 1&gt; red 2&gt; green 3&gt; blue Your favourite colour? [3]:</code></pre> <p>Note the default answer after the prompt. A user can now just hit <code>enter</code> (or set <code>$Term::UI::AUTOREPLY</code> -- see the <code>GLOBAL VARIABLES</code> section) and the sensible answer &#39;blue&#39; will be returned.</p> <h2 id="get_reply-using-print_me-&amp;-multi"><a class="permalink" href="#get_reply-using-print_me-&amp;-multi">#</a><a id="get_reply2"></a><a id="get_reply-using-print_me-multi"></a>get_reply using print_me &amp; multi</h2> <pre><code>### allow the user to pick more than one colour and add an ### introduction text @reply = $term-&gt;get_reply( print_me =&gt; &#39;Tell us what colours you like&#39;, prompt =&gt; &#39;Your favourite colours?&#39;, choices =&gt; [qw|red green blue|], multi =&gt; 1 );</code></pre> <p>which would look like:</p> <pre><code class="plaintext">Tell us what colours you like 1&gt; red 2&gt; green 3&gt; blue Your favourite colours?</code></pre> <p>An answer of <code>3 2 1</code> would fill <code>@reply</code> with <code>blue green red</code></p> <h2 id="get_reply-&amp;-allow"><a class="permalink" href="#get_reply-&amp;-allow">#</a><a id="get_reply3"></a><a id="get_reply-allow"></a>get_reply &amp; allow</h2> <pre><code>### pose an open question, but do a custom verification on ### the answer, which will only exit the question loop, if ### the answer matches the allow handler. $reply = $term-&gt;get_reply( prompt =&gt; &quot;What is the magic number?&quot;, allow =&gt; 42 );</code></pre> <p>Unless the user now enters <code>42</code>, the question will be reposed over and over again. You can use more sophisticated <code>allow</code> handlers (even subroutines can be used). The <code>allow</code> handler is implemented using <code>Params::Check</code>&#39;s <code>allow</code> function. Check its manpage for details.</p> <h2 id="an-elaborate-ask_yn-sample"><a class="permalink" href="#an-elaborate-ask_yn-sample">#</a><a id="an"></a>an elaborate ask_yn sample</h2> <pre><code>### ask a user if he likes cookies. Default to a sensible &#39;yes&#39; ### and inform him first what cookies are. $bool = $term-&gt;ask_yn( prompt =&gt; &#39;Do you like cookies?&#39;, default =&gt; &#39;y&#39;, print_me =&gt; &#39;Cookies are LOVELY!!!&#39; );</code></pre> <p>would print:</p> <pre><code class="plaintext">Cookies are LOVELY!!! Do you like cookies? [Y/n]:</code></pre> <p>If a user then simply hits <code>enter</code>, agreeing with the default, <code>$bool</code> would be set to <code>true</code>. (Simply hitting &#39;y&#39; would also return <code>true</code>. Hitting &#39;n&#39; would return <code>false</code>)</p> <p>We could later retrieve this interaction by printing out the Q&amp;A history as follows:</p> <pre><code>print $term-&gt;history_as_string;</code></pre> <p>which would then print:</p> <pre><code class="plaintext">Cookies are LOVELY!!! Do you like cookies? [Y/n]: y</code></pre> <p>There&#39;s a chance we&#39;re doing this non-interactively, because a console is missing, the user indicated he just wanted the defaults, etc.</p> <p>In this case, simply setting <code>$Term::UI::AUTOREPLY</code> to true, will return from every question with the default answer set for the question. Do note that if <code>AUTOREPLY</code> is true, and no default is set, <code>Term::UI</code> will warn about this and return <code>undef</code>.</p> <h1 id="See-Also"><a class="permalink" href="#See-Also">#</a><a id="See"></a>See Also</h1> <p><code>Params::Check</code>, <code>Term::ReadLine</code>, <code>Term::UI::History</code></p> <h1 id="BUG-REPORTS"><a class="permalink" href="#BUG-REPORTS">#</a><a id="BUG"></a>BUG REPORTS</h1> <p>Please report bugs or other issues to &lt;bug-term-ui@rt.cpan.org&lt;gt&gt;.</p> <h1 id="AUTHOR"><a class="permalink" href="#AUTHOR">#</a>AUTHOR</h1> <p>This module by Jos Boumans &lt;kane@cpan.org&gt;.</p> <h1 id="COPYRIGHT"><a class="permalink" href="#COPYRIGHT">#</a>COPYRIGHT</h1> <p>This library is free software; you may redistribute and/or modify it under the same terms as Perl itself.</p> </div> <div id="footer"> <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p> <p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/js/highlight.pack.js"></script> <script>hljs.highlightAll();</script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10