| Developer's Daily | Perl Education |
| front page | java | perl | unix | dev directory | web log |
Introduction
For many Unix users, Perl has replaced the Bourne shell, C shell, and Korn shell as a preferred programming language for many scripting duties and short programs. This is because Perl provides access to all of powerful Unix built-in commands, along with a more powerful programming language than the shells provide. (The fact that it's free doesn't hurt either.)
Used as a scripting language, Perl programs often require you to prompt the user to enter some type of input. I've found that instead of writing separate "prompting" code in each of my Perl scripts, it's easier to create one prompting routine that can be used in almost all Perl programs.
In this article we'll take a look at the promptUser() function
and demonstrate it's use in a short driver program.
Capabilities of the promptUser() function
Before going any farther, let's summarize the capabilities of the promptUser() function:
The blackbox view of the function
Example 1 - Providing only the $promptString as input
The promptUser() function accepts one or two arguments as input. The first argument is the text string that you want to use as a prompt. This can often be something like "Enter your age", or "Enter the home directory". Here's how you'd call the function in a Perl program to prompt the user to enter their home directory:
Example 2 - Providing both the $promptString and a $defaultValue as input arguments
In a second example, suppose you're writing a Perl program to install user accounts. Assuming you already know that a user's name is fred, and the normal home directory for a user named fred would be /home/fred, you might call the promptUser() function with these two arguments:
On to the code
Okay, now that you've seen how the promptUser function works, and what it's output looks like, let's look at the code. The source code for the promptUser function and a simple driver program is shown in Listing 1. (A driver program is often used to test functions and methods when they're first created, or when they're being debugged.) Because the code is well-documented, we'll only discuss its key features.
#!/usr/local/bin/perl
#----------------------------------------------------------------------#
# Copyright 1998 by DevDaily Interactive, Inc. All Rights Reserved. #
#----------------------------------------------------------------------#
#------------------------------------------#
# "driver" program to test &promptUser() #
#------------------------------------------#
$username = &promptUser("Enter the username ");
$password = &promptUser("Enter the password ");
$homeDir = &promptUser("Enter the home directory ", "/home/$username");
print "$username, $password, $homeDir\n";
#----------------------------( promptUser )-----------------------------#
# #
# FUNCTION: promptUser #
# #
# PURPOSE: Prompt the user for some type of input, and return the #
# input back to the calling program. #
# #
# ARGS: $promptString - what you want to prompt the user with #
# $defaultValue - (optional) a default value for the prompt #
# #
#-------------------------------------------------------------------------#
sub promptUser {
#-------------------------------------------------------------------#
# two possible input arguments - $promptString, and $defaultValue #
# make the input arguments local variables. #
#-------------------------------------------------------------------#
local($promptString,$defaultValue) = @_;
#-------------------------------------------------------------------#
# if there is a default value, use the first print statement; if #
# no default is provided, print the second string. #
#-------------------------------------------------------------------#
if ($defaultValue) {
print $promptString, "[", $defaultValue, "]: ";
} else {
print $promptString, ": ";
}
$| = 1; # force a flush after our print
$_ = <STDIN>; # get the input from STDIN (presumably the keyboard)
#------------------------------------------------------------------#
# remove the newline character from the end of the input the user #
# gave us. #
#------------------------------------------------------------------#
chomp;
#-----------------------------------------------------------------#
# if we had a $default value, and the user gave us input, then #
# return the input; if we had a default, and they gave us no #
# no input, return the $defaultValue. #
# #
# if we did not have a default value, then just return whatever #
# the user gave us. if they just hit the <enter> key, #
# the calling routine will have to deal with that. #
#-----------------------------------------------------------------#
if ("$defaultValue") {
return $_ ? $_ : $defaultValue; # return $_ if it has a value
} else {
return $_;
}
}
|
| Listing 1: | The promptUser() function can be used to prompt users for input from many Perl programs. |
Next comes the promptUser() function. As discussed, the promptUser() function accepts two arguments as input - $promptString and $defaultValue. These two values are converted to local values when they're created in the first step of the function. As we all know, this is much better than using global variables.
Next, the prompt is displayed to the user. If a $defaultValue was provided, the first print statement will be used; otherwise the second print statement will be used. (Remember that $defaultValue is treated as false if it's value is zero or null.)
Then we set the special variable $| to a non-zero value to flush the output, and then read the input from STDIN ("standard input"). Assuming that this function is used in an interactive program, STDIN will be the user's keyboard. After the data is read, the chomp function is called to remove any newline characters at the end of the user input.
At the end of the function we again check the value of $defaultValue. If a default value was provided, we check the user input. If the user just hit the <Enter> key, we return the value of $defaultValue. If the user entered some type of input, we'll return $_ instead. That's what happens in this syntax:
Download the promptUser() source code
The best way to learn about programming and a function like this is to use it and see if it fits your needs. We encourage you to try this function, and we welcome your comments and suggestions on improving the code.
To download the prompt.pl source code shown in Listing 1, click here. Once the file is displayed in your browser you can select the File | Save As ... option of your browser to save the code to your local filesystem.
If you have any questions or comments about this code, click
here to write us. We hope you enjoyed this article, and find
it useful for your Perl applications!
Copyright © 1998 DevDaily Interactive, Inc.
All Rights Reserved.