Using Term::Prompt for terminal output from Perl

less than 1 minute read

For a script I made recently to manage contacts to my LDAP server on the command line, I needed a package to easily output a prompt to the terminal.

The requirement is to be able to output things very easily and giving a default value that will be returned. After checking out some packages at CPAN, I decided to go for Term::Prompt. It offer a very convenient interface, basic checks for the returned value and works as I like it to. The code

$result = prompt('X', 'first name', '', 'John Doe' );

Will output

first name [default John Doe]

There’s just one thing that took a while to figure out: Some prompt don’t really need an input of course, e.g. not all phone numbers shall be filled and there is not default value.

But reading the perldoc carefully revealed that you simply have to use capitalise prompt types.

Capitalizing the type of prompt will also mean that a return may be accepted as a response, even if there is no default; whether it actually is will depend on the type of prompt. Menus, for example, do not do this.

OK, as ususal RTFM helps…