472,954 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

Read from terminal using Perl

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
9 11760
KevinADC
4,059 Expert 2GB
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
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
4,059 Expert 2GB
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
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
3,509 Expert Mod 2GB
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
4,059 Expert 2GB
The problem must be in how the perl script is being run from the other application.
Jul 8 '08 #7
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
4,059 Expert 2GB
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
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

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

Similar topics

2
by: Sam | last post by:
I would like to store html templates in a database. By using perl I would like to retrive the template ask the user to fill the template and store the whole file is template + the user data in a...
0
by: harshan | last post by:
Hi I am trying to read required worksheet from excel file using perl with module spreadsheet::parseexcel and spreadsheet::writeexcel but I am unable to read it, the error I am getting is...
6
by: atinti | last post by:
I'm tyring to write something that will send a simple email using Perl so far this is what I have #!/usr/bin/perl -w use strict; my $executable = "saplotus.exe'; my $server =...
2
by: barrybevel | last post by:
Hi, I have a very small simple program below which does the following: 1) post a username & password to a website - THIS WORKS 2) follow a link - THIS WORKS 3) update values of 2 fields and...
8
crazy4perl
by: crazy4perl | last post by:
hi all !!! i m new to perl. can anyone here please tell me how to communicate with websites using perl. do we need to use CGI for that or simply by using perl... simply i just wnt to fill some...
3
by: Max58kl | last post by:
Trying to access data and print it to the screen using Perl Builders I/O Window -------------------------------------------------------------------------------- Hi I am using a program called...
3
by: poolboi | last post by:
hi guys, i dunno if this post should be in Perl or MySQL but i'm using Perl DBI to do manupilations now in MySQL i've got problem trying to input 2 arrays of data into 2 fields at the same...
3
by: rabtree | last post by:
Hi All, I have to two xls file named test1.xls and test2.xls In test1.xls sheet contain 3 colunm 1.Sr.No 2.Case Name 3.Validation case
0
by: koti688 | last post by:
Hi Mates, Can u Please tell me how to connect to Berkely Db using Perl . I have two files , i need to access these file using perl. These files with .db extension contains Keys and its...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.