ROB6789 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i have the windows xp professional operating system and activeperl (which i'm comming to learn makes perl much harder). How would i create a program that asks a question, (like, is the rock blue? yes or no), and get a user input in of yes or no. if the user typed in "yes" then how would i get it to say something like "great, its blue" and if the user typed in "no" then how would i get it to say something like "that stincks." (please give repsonses so that a nube to perl, and a 12 year old would understand them)... because... that is what i am
  • Comment on How do i create different commands for different user inputs

Replies are listed 'Best First'.
Re: How do i create different commands for different user inputs
by Tanktalus (Canon) on Feb 06, 2005 at 05:51 UTC

    ROB,

    Welcome to the world of programming. ;-) I would highly recommend some basic programming books or sites - they will explain details like this so much better than some of the riff-raff here (say, me). They'll lead you right through this type of hurdle. That's not to say people here won't give you the right answer, but the books will often go through many of the details and nuances of programming that will help you answer the question on your own later. Merely seeing the answer isn't enough - you have to understand it, and that's often what is lacking in many of the responses here, mine included. I would suggest that you look at the O'Reilly Perl on CD set - it should get you through all stages of perl programming. Or at least most of them. Good luck!

    $|++; # make sure we turn off buffering. print "Is the rock blue? yes or no: "; my $response = <STDIN>; chomp $response; # get rid of the enter key if ($response eq 'yes') { print "Great, it's blue!\n"; } else { print "Hmm - that stinks. You need a blue rock.\n"; }
      Your code only checks for "yes". If the user entered "Sure!", you would get the "Hmm - that stinks. You need a blue rock." answer. Better test for "no" too:
      $|++; # make sure we turn off buffering. print "Is the rock blue? yes or no: "; my $response = <STDIN>; chomp $response; # get rid of the enter key if ($response eq 'yes') { print "Great, it's blue!\n"; } elsif ($response eq 'no') { print "Hmm - that stinks. You need a blue rock.\n"; } else { print "yes or no, boy!\n"; }

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: How do i create different commands for different user inputs
by punkish (Priest) on Feb 06, 2005 at 05:49 UTC
    Rob, The best thing you can do is to look at some of the basic tutorials right here on perlmonks or elsewhere on the web. There are gazillions of them. If possible, grab a copy of Learning Perl or any other beginner's Perl learning book. Do a few tutorials... should take less than a day. Your current question will be answered and then some. Btw, you are making a great start. When I was 12 I had no idea what a computer was...
Re: How do i create different commands for different user inputs
by Errto (Vicar) on Feb 06, 2005 at 05:58 UTC

    Hi ROB6789,

    Based on your last post, you already know how to get some input from the user through the console (that's a Unix term I guess, in Windows it's the command prompt). In Perl, you use an "if" statement to have your program do different things based on a condition. And "eq" is how you compare two strings to see if they are equal. So you could say

    my $var = <STDIN>; if ($var eq "something") { # this code only gets called if $var contains "something" } else { # this code only gets called if $var doesn't contain "something" }
    Based on your question, I'm guessing you haven't programmed much before. In that case, Perl maybe isn't the best choice of a first language. But if you want to stick with Perl, you should buy the book Learning Perl by Randal Schwartz. It's a great first book for Perl.