Read from terminal using Perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bmerlover
    New Member
    • Jul 2007
    • 49

    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.

    Code:
    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!";
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    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.

    Comment

    • bmerlover
      New Member
      • Jul 2007
      • 49

      #3
      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.

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Try this just to see if it does work:

        Code:
        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";
        }

        Comment

        • bmerlover
          New Member
          • Jul 2007
          • 49

          #5
          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

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            Originally posted by bmerlover
            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

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              The problem must be in how the perl script is being run from the other application.

              Comment

              • bmerlover
                New Member
                • Jul 2007
                • 49

                #8
                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?

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  Originally posted by bmerlover
                  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).

                  Code:
                  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.

                  Comment

                  • bmerlover
                    New Member
                    • Jul 2007
                    • 49

                    #10
                    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!

                    Comment

                    Working...