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. -
-
print "Choose a means of transportation:\n\n";
-
print "1. car\n2. bike\n 3. airplane\n\nSelect Option: ";
-
$result = <STDIN>;
-
-
if ($result == 1)
-
{
-
print "\nYou chose a car\n";
-
}
-
elsif($result == 2)
-
{
-
print "\nYou chose a bike\n";
-
}
-
elsif($result == 3)
-
{
-
print "\nYou chose an airplane\n";
-
}
-
print "\nHave a safe trip!";
-
9 11760
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.
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.
Try this just to see if it does work: - print "Choose a means of transportation:\n\n";
-
print "Select Option 1, 2 or 3 - 1. (car) 2. (bike) 3. (airplane) ";
-
chomp ($result = <STDIN>);
-
if ($result == 1){
-
print "\nYou chose a car\nHave a safe trip!";
-
}
-
elsif($result == 2){
-
print "\nYou chose a bike\nHave a safe trip!";
-
}
-
elsif($result == 3){
-
print "\nYou chose an airplane\nHave a safe trip!";
-
}
-
else {
-
print "\nInvalid Option Selected. Retry\n";
-
}
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
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
The problem must be in how the perl script is being run from the other application.
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?
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). - print "Please input something: "; # this will wait for input
-
-
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.
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!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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=()=>{
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |