ADVERTISEMENT
ADVERTISEMENT

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.

ADVERTISEMENT




ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.