How to get user input in Perl?

Here, we are going to learn how to get user input in Perl?
Submitted by Godwill Tetah, on December 02, 2020

In Perl, data can be gotten from a user and stored in a variable or hash. This is done using the <STDIN> command.

Below is a code to get user input and store in a variable:

Program/Source Code:

File name: index.pl

#Getting data from a user in Perl

print "what is your name? \n";
$x = <STDIN>;

print "Welcome Mr $x";

Output:

what is your name?
Godwill
Welcome Mr Godwill

The user input could also be assigned to a hash or array by simply replacing the variable with a hash or array. Doing this will give the user the possibility to add multiple data inputs since arrays and hashes don't only store one value. For Example;

#Gettings data from a user in Perl

print "what is your name? \n";
@x = <STDIN>;

print "@x";

Output:

what is your name?
Godwill
Tetah
IncludeHelp

Since the output runs on a command line, use the command for your platform equivalent to Ctrl + C on windows to the list input.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.