my $reply = $term->get_reply( prompt => 'What is your favourite colour?', choices => [qw blue red green ], default => blue, );

Similar documents
use Log::Message private => 0, config => '/our/cf_file'; my $log = Log::Message->new( private => 1, level => 'log', config => '/my/cf_file', );

use Params::Check qw[check allow last_error];

$bool = $obj->mk_aliases( # create an alias to an existing alias_name => 'method'); # method name

my $full_path = can_run('wget') or warn 'wget is not installed!';

my $full_path = can_run('wget') or warn 'wget is not installed!';

NAME SYNOPSIS. Perl version documentation - Pod::Parser. Pod::Parser - base class for creating POD filters and translators.

NAME SYNOPSIS. Perl version documentation - Pod::Parser. Pod::Parser - base class for creating POD filters and translators.

### build a File::Fetch object ### my $ff = File::Fetch->new(uri => '

pairs unpairs pairkeys pairvalues pairfirst pairgrep pairmap

Pod::Usage, pod2usage() - print a usage message from embedded pod documentation

The Perl Debugger. Avoiding Bugs with Warnings and Strict. Daniel Allen. Abstract

package YourModule; require = = qw(munge frobnicate); # symbols to export on request

Module::Pluggable - automatically give your module the ability to have plugins

@EXPORT_OK = qw(munge frobnicate); # symbols to export on request

runs all the testscripts named as arguments and checks standard output for the expected strings in TAP format.

*roff code is suitable for display on a terminal using nroff(1), normally via man(1), or printing using troff(1).

This document describes version 2.10 of Switch, released Dec 29, 2003.

[Skip ahead to DESCRIPTION if you don't care about the whys and wherefores of this control structure]

Tieing and Overloading Objects in Perl. Dave Cross Magnum Solutions

Subroutines. Subroutines. The Basics. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc.

AHHHHHHH!!!! NOT TESTING! Anything but testing! Beat me, whip me, send me to Detroit, but don t make me write tests!

This document describes version 0.87 of Attribute::Handlers, released September 21, 2009.

1 Apache2::RequestUtil - Perl API for Apache

Fortunately, the layout is much more legible, more like BASIC's PRINT USING statement. Think of it as a poor man's nroff(1). nroff

print STDERR "This is a debugging message.\n";

Command line options come in several flavours. Historically, they are preceded by a single dash -, and consist of a single letter.

1 Apache2::Filter - Perl API for Apache 2.0 Filtering

### build an Archive::Extract object ### my $ae = Archive::Extract->new( archive => 'foo.tgz' );

# use a BEGIN block so we print our plan before MyModule is loaded BEGIN { plan tests => 14, todo => [3,4] }

CS 105 Perl: Completing the Toolbox

Chapter 1 Getting Started

Voice Messaging Instructions Contents

Magit-Popup User Manual

Homework 2 CS161 Computer Security, Spring 2008 Assigned 2/13/08 Due 2/25/08

Perl Tutorial. Diana Inkpen. School of Information Technology and Engineering University of Ottawa. CSI 5180, Fall 2004

$ftp = Net::FTP->new("some.host.name", Debug => 0) or die "Cannot connect to some.host.name:

subtest $builder->subtest($name,

Shell Scripting. Todd Kelley CST8207 Todd Kelley 1

Introduction to Perl Session 6. special variables subroutines Introduction to Perl

use File::Find; find({ wanted => \&process, follow => 1 }, '.');

Data::Dumper - stringified perl data structures, suitable for both printing and eval

Data::Dumper - stringified perl data structures, suitable for both printing and eval

Using WebBoard at UIS

Scripting Languages Perl Basics. Course: Hebrew University

Business Manager Net. Supplemental Manual

Please post bugs on our forum on or us on

$syntax_okay = podchecker($filepath, $outputpath, %options);

PCS 552 Quick Reference Guide

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

$syntax_okay = podchecker($filepath, $outputpath, %options);

COMS 3101 Programming Languages: Perl. Lecture 6

CS 1110, LAB 1: PYTHON EXPRESSIONS.

print inf + 42,"\n"; print NaN * 7,"\n"; print hex("0x "),"\n"; # Perl v or later

Pathologically Eclectic Rubbish Lister

PCS 560 Quick Reference Guide

Pod::Simple::HTMLBatch - convert several Pod files to several HTML files. perl -MPod::Simple::HTMLBatch -e 'Pod::Simple::HTMLBatch::go' in out

SSH SECURITY. If you ve never used SSH before on a computer, the chances are very high that

# start with SSL, e.g. nntps $nntp = Net::NNTP->new("some.host.name", SSL => 1);

Learning Perl 6. brian d foy, Version 0.6, Nordic Perl Workshop 2007

Operating System Interaction via bash

use attributes (); # optional, to get subroutine declarations = attributes::get(\&foo);

1 Apache2::ServerUtil - Perl API for Apache server

Command line options come in several flavours. Historically, they are preceded by a single dash -, and consist of a single letter.

Visual Streamline FAQ

AutoArchive. Release 1.4.1

A shell can be used in one of two ways:

Be sure to read ENCODING if your Pod contains non-ascii characters.

The following content is provided under a Creative Commons license. Your support

autodie - Replace functions with ones that succeed or die with lexical scope # Recommended: implies 'use autodie qw(:default)'

Python for Non-programmers

CS102. Lecture 7. xkcd. xkcd. palindramas

A Field Guide To The Perl Command Line. Andy Lester

Perl (5 Days Content)

IN CHAPTER 7, SUBROUTINES AND MODULES, you learned how to organize

Ticketing Service 1 Request Tracker (RT) Installation and Configuration

$pat = '(?{ $foo = 1 })'; use re 'eval'; /foo${pat}bar/; # won't fail (when not under -T # switch)

1. Introduction. 2. Scalar Data

Job Workflow. A step by step guide to

$parser->parse_from_file ('file.pod', 'file.tex');

Table of Contents Data Validation... 2 Data Validation Dialog Box... 3 INDIRECT function... 3 Cumulative List of Keyboards Throughout Class:...

Increment and the While. Class 15

OpenScape Business V2 OpenScape Business Attendant. User Guide A31003-P3010-U

PCS 563. l PCS 563. Quick Reference guide. April Enhanced Handset Alpha-Numeric Keypad. 9 x Context Sensitive Keys. Context Sensitive Display

NEXT - Provide a pseudo-class NEXT (et al) that allows method redispatch

Understanding the KE IMu Server

Project 5 - The Meta-Circular Evaluator

argcomplete Documentation

pydocstyle Documentation

$tar->add_files('file/foo.pl', 'docs/readme'); $tar->add_data('file/baz.txt', 'This is the contents now');

use CGI::Carp qw(fatalstobrowser); die "Fatal error messages are now sent to browser";

The Design Process. General Development Issues. C/C++ and OO Rules of Thumb. Home

Computer Science is...

Organising your inbox

PERL DATABASE ACCESS

Arguments of the use overload directive are (key, value) pairs. For the full set of legal keys, see Overloadable Operations below.

Object Oriented Programming and Perl

CS 360 Programming Languages Interpreters

Harvard Phone. Introduction to Contact Center CONTACT CENTER CLIENT QUICK REFERENCE QUIDE

Title: Counting While Loops

Libgdb. Version 0.3 Oct Thomas Lord

Transcription:

NAME SYNOPSIS Term::UI - Term::ReadLine UI made easy use Term::UI; use Term::ReadLine; my $term = Term::ReadLine->new('brand'); my prompt => 'What is your favourite colour?', choices => [qw blue red green ], default => blue, ); my $bool = $term->ask_yn( prompt => 'Do you like cookies?', default => 'y', ); my $string = q[some_command -option --no-foo --quux='this thing']; my ($options,$munged_input) = $term->parse_options($string); ### don't have Term::UI issue warnings -- default is '1' $Term::UI::VERBOSE = 0; ### always pick the default (good for non-interactive terms) ### -- default is '0' $Term::UI::AUTOREPLY = 1; DESCRIPTION ### Retrieve the entire session as a printable string: $hist = Term::UI::History->history_as_string; $hist = $term->history_as_string; Term::UI 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. Simply give it the question you want to ask, optionally with choices the user can pick from and a default and Term::UI will DWYM. HOW IT WORKS For asking a yes or no question, there's even a shortcut. Term::UI places itself at the back of the Term::ReadLine @ISA array, so you can call its functions through your term object. Term::UI uses Term::UI::History to record all interactions with the commandline. You can retrieve this history, or alter the filehandle the interaction is printed to. See the Term::UI::History manpage or the SYNOPSIS for details. Page 1

METHODS prompt => 'question?', [choices => \@list, default => $list[0], multi => BOOL, print_me => "extra text to print & record", allow => $ref] ); get_reply 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. You have the option of providing a list of choices the user can pick from using the choices argument. If the answer is not in the list of choices presented, the question will be reposed. If you provide a default answer, this will be returned when either $AUTOREPLY is set to true, (see the GLOBAL VARIABLES section further below), or when the user just hits enter. You can indicate that the user is allowed to enter multiple answers by toggling the multi flag. Note that a list of answers will then be returned to you, rather than a simple string. By specifying an allow hander, you can yourself validate the answer a user gives. This can be any of the types that the Params::Check allow function allows, so please refer to that manpage for details. Finally, you have the option of adding a print_me argument, which is simply printed before the prompt. It'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&A with the user, and retrieve it later using the Term::UI->history_as_string function. See the EXAMPLES section for samples of how to use this function. $bool = $term->ask_yn( prompt => "your question", [default => (y 1,n 0), print_me => "extra text to print & record"] ) Asks a simple yes or no question to the user, returning a boolean indicating true or false to the caller. The default answer will automatically returned, if the user hits enter or if $AUTOREPLY is set to true. See the GLOBAL VARIABLES section further below. Also, you have the option of adding a print_me argument, which is simply printed before the prompt. It'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&A with the user, and retrieve it later using the Term::UI->history_as_string function. See the EXAMPLES section for samples of how to use this function. ($opts, $munged) = $term->parse_options( STRING ); parse_options 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. Consider this example: my $str = q[command --no-foo --baz --bar=0 --quux=bleh ]. q[--option="some'thing" -one-dash -single=blah' arg]; my ($options,$munged) = $term->parse_options($str); ### $options would contain: ### $options = { 'foo' => 0, 'bar' => 0, 'one-dash' => 1, 'baz' => 1, 'quux' => 'bleh', Page 2

}; 'single' 'option' => 'blah\'', => 'some\'thing' ### and this is the munged version of the input string, ### ie what's left of the input minus the options $munged = 'command arg'; As you can see, you can either use a single or a double - to indicate an option. If you prefix an option with no- 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. $str = $term->history_as_string Convenience wrapper around Term::UI::History->history_as_string. Consult the Term::UI::History man page for details. GLOBAL VARIABLES The behaviour of Term::UI can be altered by changing the following global variables: $Term::UI::VERBOSE 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; $Term::UI::AUTOREPLY 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; $Term::UI::INVALID 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 Invalid selection, please try again: $Term::UI::History::HISTORY_FH This is the filehandle all the print statements from this module are being sent to. Please consult the Term::UI::History manpage for details. This defaults to *STDOUT. EXAMPLES Basic get_reply sample ### ask a user (with an open question) for their favourite colour prompt => 'Your favourite colour? ); Your favourite colour? and $reply would hold the text the user typed. get_reply with choices ### now provide a list of choices, so the user has to pick one prompt => 'Your favourite colour?', choices => [qw red green blue ] ); Page 3

Your favourite colour? $reply will hold one of the choices presented. Term::UI will repose the question if the user attempts to enter an answer that's not in the list of choices. The string presented is held in the $Term::UI::INVALID variable (see the GLOBAL VARIABLES section for details. get_reply with choices and default ### provide a sensible default option -- everyone loves blue! prompt => 'Your favourite colour?', choices => [qw red green blue ], default => 'blue' ); Your favourite colour? [3]: Note the default answer after the prompt. A user can now just hit enter (or set $Term::UI::AUTOREPLY -- see the GLOBAL VARIABLES section) and the sensible answer 'blue' will be returned. get_reply using print_me & multi ### allow the user to pick more than one colour and add an ### introduction text @reply = $term->get_reply( print_me => 'Tell us what colours you like', prompt => 'Your favourite colours?', choices => [qw red green blue ], multi => 1 ); Tell us what colours you like Your favourite colours? get_reply & allow An answer of 3 2 1 would fill @reply with blue green red ### pose an open question, but do a custom verification on ### the answer, which will only exit the question loop, if Page 4

### the answer matches the allow handler. prompt => "What is the magic number?", allow => 42 ); Unless the user now enters 42, the question will be reposed over and over again. You can use more sophisticated allow handlers (even subroutines can be used). The allow handler is implemented using Params::Check's allow function. Check its manpage for details. an elaborate ask_yn sample ### ask a user if he likes cookies. Default to a sensible 'yes' ### and inform him first what cookies are. $bool = $term->ask_yn( prompt => 'Do you like cookies?', default => 'y', print_me => 'Cookies are LOVELY!!!' ); would print: Cookies are LOVELY!!! Do you like cookies? [Y/n]: If a user then simply hits enter, agreeing with the default, $bool would be set to true. (Simply hitting 'y' would also return true. Hitting 'n' would return false) We could later retrieve this interaction by printing out the Q&A history as follows: print $term->history_as_string; which would then print: Cookies are LOVELY!!! Do you like cookies? [Y/n]: y See Also BUG REPORTS AUTHOR COPYRIGHT There's a chance we're doing this non-interactively, because a console is missing, the user indicated he just wanted the defaults, etc. In this case, simply setting $Term::UI::AUTOREPLY to true, will return from every question with the default answer set for the question. Do note that if AUTOREPLY is true, and no default is set, Term::UI will warn about this and return undef. Params::Check, Term::ReadLine, Term::UI::History Please report bugs or other issues to <bug-term-ui@rt.cpan.org<gt>. This module by Jos Boumans <kane@cpan.org>. This library is free software; you may redistribute and/or modify it under the same terms as Perl itself. Page 5