By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,182 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

Read from terminal using Perl

P: 50
I would like to know how to prompt the user for an input on the terminal. I have a print statement which asks for 3 choices and use <STDIN> to prompt the user for an input. This code works perfectly with DOS but I need it to work on the terminal. I don't know how to read from terminal. I looked up some shell scripts and it says read CMD but that command doesn't work in Perl.

Expand|Select|Wrap|Line Numbers
  1.  
  2. print "Choose a means of transportation:\n\n";
  3. print "1. car\n2. bike\n 3. airplane\n\nSelect Option: ";
  4. $result = <STDIN>;
  5.  
  6. if ($result == 1)
  7. {
  8.      print "\nYou chose a car\n";
  9. }
  10. elsif($result == 2)
  11. {
  12.      print "\nYou chose a bike\n";
  13. }
  14. elsif($result == 3)
  15. {
  16.      print "\nYou chose an airplane\n";
  17. }
  18. print "\nHave a safe trip!";
  19.  
Jul 7 '08 #1
Share this Question
Share on Google+
9 Replies


KevinADC
Expert 2.5K+
P: 4,059
Do you mean use a GUI? Like the use enters the data in a textbox of some sort? There is tk and tkx for that purpose.
Jul 8 '08 #2

P: 50
The program itself is not a GUI. This perl application is called from some other script and the perl application displays the results in the terminal. I just want it to ask the user a question in the terminal and depending on the choice the user selects, it takes the a specific route to find an alternative. This is run on Solaris 8 OS. The script itself with slight modifications works find on a windows xp operating system using the command prompt. It asks the user in the command prompt, depending on the users choice it takes a specific route. <STDIN> pauses the application and prompts the user to select a number in the command prompt. Using <STDIN> doesn't work on the terminal, it doesn't stop the application, it just continues and the program finishes.
Jul 8 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
Try this just to see if it does work:

Expand|Select|Wrap|Line Numbers
  1. print "Choose a means of transportation:\n\n";
  2. print "Select Option 1, 2 or 3 - 1. (car)  2. (bike)  3. (airplane) ";
  3. chomp ($result = <STDIN>);
  4. if ($result == 1){
  5.   print "\nYou chose a car\nHave a safe trip!";
  6. }
  7. elsif($result == 2){
  8.   print "\nYou chose a bike\nHave a safe trip!";
  9. }
  10. elsif($result == 3){
  11.   print "\nYou chose an airplane\nHave a safe trip!";
  12. }
  13. else {
  14.   print "\nInvalid Option Selected. Retry\n";
  15. }     
Jul 8 '08 #4

P: 50
I just tried that and it still doesn't seem to work. It doesn't prompt the user, instead it gives these errors:

use of unitialized value in chomp at line 515.

use of unitialized value in numeric eq (==) at line 519
use of unitialized value in numeric eq (==) at line 519
use of unitialized value in numeric eq (==) at line 519

line 515 contains chomp ($result = <STDIN>);
line 519 contains the first if condition if($result == 1)

Is there any other method? or am I missing a "use" function?

I'm currently using:

#!/bin/perl

use strict;
use warnings;

I have also tried
#!/usr/bin/perl
Jul 8 '08 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
I just tried that and it still doesn't seem to work. It doesn't prompt the user, instead it gives these errors:

use of unitialized value in chomp at line 515.

use of unitialized value in numeric eq (==) at line 519
use of unitialized value in numeric eq (==) at line 519
use of unitialized value in numeric eq (==) at line 519

line 515 contains chomp ($result = <STDIN>);
line 519 contains the first if condition if($result == 1)

Is there any other method? or am I missing a "use" function?

I'm currently using:

#!/bin/perl

use strict;
use warnings;

I have also tried
#!/usr/bin/perl
Those things have nothing to do with the errors you presented. What these are telling me is the variable, "$result", is not getting set and you are trying to perform operations on a variable that has yet to be set.

Regards,

Jeff
Jul 8 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
The problem must be in how the perl script is being run from the other application.
Jul 8 '08 #7

P: 50
Yes, I think you're right KevinADC. It somehow doesn't have access to the terminal, its just that since this perl application displays the contents on the terminal, I thought it would also prompt the user. I will call this perl application straight from the terminal to see if <STDIN> is working. Once I find that in this case it does work, then that confirms what you are saying.

Jeff, I understand what you are saying. My reasoning is that <STDIN> is not recognized in the terminal, and since the user is suppose to enter a choice, that choice will be saved to $result. Since I purposely didn't declare $result to see what information if any is being saved from <STDIN>. This circumstance just proves that <STDIN> is not doing anything at all, rather it is skipping that and as a result $result is not being set. Hence, I am getting that error.

I appreciate the help from both of you. I will continue working on this issue and post a solution if I come across it or post what I did wrong.

Just to make sure: <STDIN> should work in the terminal correct? it's not just designed to work in the command prompt, right?
Jul 8 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
Yes, I think you're right KevinADC. It somehow doesn't have access to the terminal, its just that since this perl application displays the contents on the terminal, I thought it would also prompt the user. I will call this perl application straight from the terminal to see if <STDIN> is working. Once I find that in this case it does work, then that confirms what you are saying.

Jeff, I understand what you are saying. My reasoning is that <STDIN> is not recognized in the terminal, and since the user is suppose to enter a choice, that choice will be saved to $result. Since I purposely didn't declare $result to see what information if any is being saved from <STDIN>. This circumstance just proves that <STDIN> is not doing anything at all, rather it is skipping that and as a result $result is not being set. Hence, I am getting that error.

I appreciate the help from both of you. I will continue working on this issue and post a solution if I come across it or post what I did wrong.

Just to make sure: <STDIN> should work in the terminal correct? it's not just designed to work in the command prompt, right?

<STDIN> is not the mechanism that causes the script to stop and wait for user input in the terminal. It is a print command with no end of line terminator (\n).

Expand|Select|Wrap|Line Numbers
  1. print "Please input something: "; # this will wait for input
  2.  
  3. print "Please input something: \n"; # this will not wait for input
The terminal and the command prompt are pretty much the same thing, no? I am not sure why the perl script just keeps running instead of waiting for input when called from the othr application. It probably has nothing to do with perl though.
Jul 8 '08 #9

P: 50
KevinADC you were right, I ran the perl application from the terminal and <STDIN> pauses for the user to input something. So it's the other script that's calling this perl application that's causing <STDIN> not to operate correctly.

Again, thanks for the help guys!
Jul 9 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.