Website Scripts And Cloud Tutorials

How to read input from keyboard using perl

Byadmin

Nov 22, 2010

Last updated on February 14th, 2022 at 08:14 am

Click to rate this tutorial!
[Total: 6 Average: 1.8]

This script will show how to read numbers from keyboard and find the sum. To exit the program all you have to do is add 999. Here is the script. For your convenience I have added a sample output as well.

#!/usr/bin/perl
print "Enter the number \n";
$userinput = <STDIN>;
chomp ($userinput);
print "User typed $userinput\n";
while ($userinput != 999) {
$sum=$sum+$userinput;
print "The SUM is $sum\n";
print "Enter the number again \n";
$userinput = <STDIN>;
chomp ($userinput);
}
print "Typed 999, So exiting\n";

Sample Output

:~# ./test.pl
Enter the number
10
User typed 10
The SUM is 10
Enter the number again
20
The SUM is 30
Enter the number again
10
The SUM is 40
Enter the number again
4
The SUM is 44
Enter the number again
45
The SUM is 89
Enter the number again
100
The SUM is 189
Enter the number again
999
Typed 999, So exiting
:~# 
Click to rate this tutorial!
[Total: 6 Average: 1.8]

Leave a Reply

Your email address will not be published.